如果希望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; } }
本文所实现的效果如图所示:
本文的源代码如下:
更多资源
C1FlexGrid for Winform中文帮助文档地址:
/docs/componentonestudio/flexgrid/
如果你对C1FlexGrid感兴趣,请到我们的官网下载最新版本:/download/?pid=3
如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-68.html