C1TrueGrid 添加行索引

我们经常收到用户的反馈,询问是否能在 C1TrueGrid 中实现类似于 Excel 程序中的行索引。行头用于指示当前活跃单元格所在行。目前,C1TrueGrid 并没有内置行头功能。在本篇博客中,我们将自定义实现该功能。

我们需要添加额外列来显示行头,我们需要定制其外观。我们将使用 UnboundColumnFetch 事件去显示当前行索引。代码如下:

private void c1TrueDBGrid1_UnboundColumnFetch(object sender, C1.Win.C1TrueDBGrid.UnboundColumnFetchEventArgs e)
{
   if (e.Row == row)
   {
     e.Value = "";
   }
   else
   {
     if (!filter)
      {
         e.Value = (e.Row + 1).ToString();
      }
      else
      {
         if (!filterdone)
          {
            counter++;
            for (int i = filindex; i < filtercol.Length; i++)
            {
              if (filtercol[ i ] == c1TrueDBGrid1.Columns[c1TrueDBGrid1.Col].FilterText || filtercol[ i ].StartsWith(c1TrueDBGrid1.Columns[c1TrueDBGrid1.Col].FilterText))
              {
                e.Value = rowindex[ i ];
                filindex = i + 1;
                filteredindexes[counter - 1] = rowindex<em></em>;
                break;
              }
            }
            if (counter == c1TrueDBGrid1.RowCount)
            {
              counter = 0;
              filterdone = true;
            }
         }
         else
         {
           e.Value = filteredindexes[e.Row];
         }
       }
     }
   }

 下一步我们需要添加当前行头指示器,这里通过 OwnerDrawCell 事件添加。代码如下:

this.c1TrueDBGrid1.OwnerDrawCell += (ss, ee) =>
{
  if (ee.Row == row)
   ee.Style.BackgroundImage = Image.FromFile(@"..\..\Images\Rec_Sel.png");
};

 通过以上两步,基本实现了行头功能,我们现在要考虑的是,进行过滤操作后,显示正确的行头。代码如下:

private void c1TrueDBGrid1_AfterFilter(object sender, C1.Win.C1TrueDBGrid.FilterEventArgs e)
{
  if (e.Condition != "")
  {
   filter = true;
   filindex = 0;
   filterdone = false;
   filteredindexes = new string[c1TrueDBGrid1.RowCount];
  }
  else
  {
   filter = false;
  }
}
private void c1TrueDBGrid1_BeforeColEdit(object sender, C1.Win.C1TrueDBGrid.BeforeColEditEventArgs e)
{
if (c1TrueDBGrid1.FilterActive && c1TrueDBGrid1.Columns[e.ColIndex].FilterText == "")
{
   filtercol = new string[c1TrueDBGrid1.RowCount];
   rowindex = new string[c1TrueDBGrid1.RowCount];
   for (int r = 0; r < c1TrueDBGrid1.RowCount; r++)
   {
     rowindex[r] = c1TrueDBGrid1[r, 0].ToString();
     filtercol[r] = c1TrueDBGrid1[r,e.ColIndex].ToString();
   }
}
}

 

效果图:

159

参考代码如下:

Download C# Sample
Download VB Sample

 

 

葡萄城控件产品网站:http://www.gcpowertools.com.cn/
葡萄城技术支持论坛:http://gcdn.grapecity.com/

你可能感兴趣的:(WinForm,第三方控件,ComponentOne,开发控件)