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

转载于:https://www.cnblogs.com/C1SupportTeam/archive/2013/03/19/C1TrueGrid-%e6%b7%bb%e5%8a%a0%e8%a1%8c%e7%b4%a2%e5%bc%95.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值