OwnerDrawCell可以使用此事件自定义网格中任何单元格的外观。该事件允许三种主要类型的自定义:
1、更改文本和图像参数的值以修改网格显示的值。例如,您可以使用这种类型的自定义将密码字符串替换为星号。
2、更改“样式”特性以使用与默认情况下网格选择的样式不同的样式显示单元格。例如,您可以使用这种类型的自定义来提供条件格式。
3、使用图形和边界参数,自己绘制单元。以这种方式绘制单元格时,可以调用OwnerDrawCellEventArgs。DrawCell成员强制网格绘制单元格的特定部分,同时代码绘制其他部分。例如,可以绘制自定义背景,然后调用DrawCell让网格绘制单元格边框和内容。
当网格自动调整行或列的大小时,也会触发OwnerDrawCell事件(请参见BaseGrid.AutoSizeRows和BaseGrid.AutoSizeCols方法)。之所以这样做,是因为网格需要使用与渲染单元相同的文本、图像和样式参数来测量单元。在这些情况下,测量参数设置为true,边界矩形为空。
下面是e参数可以获得的对象清单
Vb.Net |
Public Sub SmGrid1_OwnerDrawCell(sender As Object,e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Dim tbl As SmGrid=sender '可以获得行、列值,那么就可以获得其他类型的行列信息了。 Dim intRow As Integer=e.Row Dim intCol As Integer=e.Col '其他可获得的对象,下面这些对象主要是用来绘画单元格内容的 Dim g As Graphics=e.Graphics Dim img As Image= e.Image Dim str As String=e.Text Dim style As CellStyle=e.Style Dim rec As Rectangle=e.Bounds End Sub |
C# |
public void SmGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e) { SmGrid tbl = sender as SmGrid; // 可以获得行、列值,那么就可以获得其他类型的行列信息了。 int intRow = e.Row; int intCol = e.Col; // 其他可获得的对象,下面这些对象主要是用来绘画单元格内容的 Graphics g = e.Graphics; Image img = e.Image; string str = e.Text; CellStyle style = e.Style; Rectangle rec = e.Bounds; } |
这里的样式e.Style一般都是指定一个自定义的样式来实现样式自定义,千万千万不能直接修改系统样式,因为一旦修改了一个,整个表的样式都会受影响。
第一步,我们先在表属性中添加几个样式。
Vb.Net |
Public Sub OwnerDrawCell(sender As Object,e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Dim tbl As SmGrid=sender Dim strColName As String=tbl.Cols(e.Col).Name '获得当前行 Dim dr As RowData=tbl.Rows(e.Row).GetRowData If dr IsNot Nothing Then Select Case strColName Case "数量" '如果数量大于1000,则用特殊的样式显示 If dr(strColName)>1000 Then e.Style=tbl.Styles("标记") End If Case "业务员" '我们可以随意设置想显示给用户看的文本内容 If dr(strColName)="业务员02" Then e.Text="**保密**" End If Case "图片" Dim strImgFile As String=dr(strColName).ToString() '将图片字段中的图片路径,以图片形式显示到单元格 e.Image=Sys.GetImage(strImgFile,Path.Combine(Proj.ProjectPath,"Images")) Case "图片2" Dim strImg As String=dr(strColName).ToString() Dim img As Image=Sys.GetImage(strImg,Path.Combine(Proj.ProjectPath,"Images")) If img IsNot Nothing Then '先将背景和边框画好 e.DrawCell(DrawCellFlags.Background Or DrawCellFlags.Border) '用GDI+的方式直接将图片画到单元格中去。 e.Graphics.DrawImage(img,e.Bounds) End If Case Else End Select End If End Sub |
C# |
public void OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e) { SmGrid tbl = sender as SmGrid; string strColName = tbl.Cols[e.Col].Name; // 获得当前行 RowData dr = tbl.Rows[e.Row].GetRowData; if (dr != null) { switch (strColName) { case "数量": { // 如果数量大于1000,则用特殊的样式显示 if (dr[strColName].CType<int>() > 1000) e.Style = tbl.Styles["标记"]; break; } case "业务员": { // 我们可以随意设置想显示给用户看的文本内容 if (dr[strColName].ToString() == "业务员02") e.Text = "**保密**"; break; } case "图片": { string strImgFile = dr[strColName].ToString(); // 将图片字段中的图片路径,以图片形式显示到单元格 e.Image = Sys.GetImage(strImgFile, Path.Combine(Proj.ProjectPath, "Images")); break; } case "图片2": { string strImg = dr[strColName].ToString(); Image img = Sys.GetImage(strImg, Path.Combine(Proj.ProjectPath, "Images")); if (img != null) { // 先将背景和边框画好 e.DrawCell(DrawCellFlags.Background | DrawCellFlags.Border); // 用GDI+的方式直接将图片画到单元格中去。 e.Graphics.DrawImage(img, e.Bounds); } break; } default: { break; } } } } |