C1FlexGrid的自动检索AutoSearch实现

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

发布于 2016/02/19 00:00

ComponentOne Enterprise

如果希望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

本文的源代码如下:

Flex_Searching_CS.zip

更多资源

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

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

ComponentOne Enterprise | 下载试用

ComponentOne 是一套专注于企业 .NET开发、支持 .NET Core 平台,并完美集成于 Visual Studio 的第三方控件集,包含 300 多种 .NET开发控件,提供表格数据管理、数据可视化、报表和文档、日程安排、输入和编辑、导航和布局、系统提升工具等七大功能,被誉为“.NET开发的‘瑞士军刀’”。

ComponentOne 为您提供专业的产品咨询服务,并由技术支持工程师为您1对1解答。>> 发帖提问

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态