C1FlexGrid的自动检索AutoSearch实现

发布时间:2016/02/19 00:02 发布者:alice

返回博客中心

如果希望C1FlexGrid的自动检索,并且将检索到的对象做特殊处理,就可以使用C1FlexGrid的AutoSearch属性。

本文就基于WinForm平台来介绍如何使用AutoSearch做自动检索,并将检索到的文字颜色变化。

这里面我们主要用到的就是OwnerDraw,在OwnerDrawCell事件里重画单元格,对于符合条件的单元格样式做特殊处理。

首先设置DrawMode属性为OwnerDraw,就可以触发OwnerDraw事件。

//To enable control to fire OwnerDrawCell Event 
c1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw; 

 

每次,text文字改变的时候,就会触发OwerDraw事件重画grid的每个单元格。包含搜索文字的单元格将会画成不同的颜色,和默认的不同。

void c1FlexGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{ 
//Check if OwnerDrawCell used for Searching 
if (_StartSearch) 
{ 
//Getting content of cell string 
CellContent = e.Text; 
//Check whether cell contains the Text in TextBox 
if (CellContent.Contains(tbSearchBox.Text)) 
{ 
SolidBrush brush = new SolidBrush(Color.Red); 
Single x = 0; 
//Traverse celltext and set ForeColor. 
for (int i = 0; i < CellContent.Length; i++) 
{ 
string character = CellContent[i].ToString(); 
//Color the Text of Search Result with Red 
e.Graphics.DrawString(character, c1FlexGrid1.Font, brush, e.Bounds.X + x, e.Bounds.Y); 
//Next character needs to be shifted right; 
//depending on the space occupied by the previous character as per its FontSize/FontStyle 
x += (e.Graphics.MeasureString(character, this.c1FlexGrid1.Font)).Width - 3; 
//To restrict FlexGrid to draw default cell content 
e.Handled = true; 
} 
}
} 
//Whether Search completed throughout all cells of grid 
if (e.Row == c1FlexGrid1.Rows.Count && e.Col == c1FlexGrid1.Cols.Count)
        {
          _StartSearch = false;
        }
 } 

 

本文所实现的效果如图所示:

Search_IN_Grid_Demo

本文的源代码如下:

更多资源

C1FlexGrid for Winform中文帮助文档地址:

/docs/componentonestudio/flexgrid/

如果你对C1FlexGrid感兴趣,请到我们的官网下载最新版本:/download/?pid=3

如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-68.html


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网