OwnerDrawCell可以使用此事件自定義網(wǎng)格中任何單元格的外觀。該事件允許三種主要類型的自定義:
1、更改文本和圖像參數(shù)的值以修改網(wǎng)格顯示的值。例如,您可以使用這種類型的自定義將密碼字符串替換為星號。
2、更改“樣式”特性以使用與默認情況下網(wǎng)格選擇的樣式不同的樣式顯示單元格。例如,您可以使用這種類型的自定義來提供條件格式。
3、使用圖形和邊界參數(shù),自己繪制單元。以這種方式繪制單元格時,可以調(diào)用OwnerDrawCellEventArgs。DrawCell成員強制網(wǎng)格繪制單元格的特定部分,同時代碼繪制其他部分。例如,可以繪制自定義背景,然后調(diào)用DrawCell讓網(wǎng)格繪制單元格邊框和內(nèi)容。
當網(wǎng)格自動調(diào)整行或列的大小時,也會觸發(fā)OwnerDrawCell事件(請參見BaseGrid.AutoSizeRows和BaseGrid.AutoSizeCols方法)。之所以這樣做,是因為網(wǎng)格需要使用與渲染單元相同的文本、圖像和樣式參數(shù)來測量單元。在這些情況下,測量參數(shù)設置為true,邊界矩形為空。
下面是e參數(shù)可以獲得的對象清單
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 '其他可獲得的對象,下面這些對象主要是用來繪畫單元格內(nèi)容的 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; // 其他可獲得的對象,下面這些對象主要是用來繪畫單元格內(nèi)容的 Graphics g = e.Graphics; Image img = e.Image; string str = e.Text; CellStyle style = e.Style; Rectangle rec = e.Bounds; } |
這里的樣式e.Style一般都是指定一個自定義的樣式來實現(xiàn)樣式自定義,千萬千萬不能直接修改系統(tǒng)樣式,因為一旦修改了一個,整個表的樣式都會受影響。
第一步,我們先在表屬性中添加幾個樣式。
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 "數(shù)量" '如果數(shù)量大于1000,則用特殊的樣式顯示 If dr(strColName)>1000 Then e.Style=tbl.Styles("標記") End If Case "業(yè)務員" '我們可以隨意設置想顯示給用戶看的文本內(nèi)容 If dr(strColName)="業(yè)務員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 "數(shù)量": { // 如果數(shù)量大于1000,則用特殊的樣式顯示 if (dr[strColName].CType<int>() > 1000) e.Style = tbl.Styles["標記"]; break; } case "業(yè)務員": { // 我們可以隨意設置想顯示給用戶看的文本內(nèi)容 if (dr[strColName].ToString() == "業(yè)務員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; } } } } |