For cases where complex per-cell customizations need to be performed you can render the contents of the cell by writing a handler for the OwnerDrawCell event. This event is raised as needed to display the contents of cells that have their OwnerDraw property set to True.
To create the owner-drawn cells in the above illustration, complete the following:
In the Designer
In Code
Add the following code to the Form_Load event:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Me.C1TrueDBGrid1.Splits(0).DisplayColumns("First").OwnerDraw = True |
To write code in C#
C# |
Copy Code
|
---|---|
this.c1TrueDBGrid1.Splits[0].DisplayColumns["First"].OwnerDraw = true; |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Public Structure RECT Dim Left As Long Dim Top As Long Dim Right As Long Dim Bottom As Long End Structure |
To write code in C#
C# |
Copy Code
|
---|---|
public struct RECT{ long Left; long Top; long Right; long Bottom; } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TrueDBGrid1_OwnerDrawCell(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs) Handles C1TrueDBGrid1.OwnerDrawCell If e.Col = 0 Then ' Create a gradient brush, blue to red. Dim pt1, pt2 As Point pt1 = New Point(e.CellRect.X, e.CellRect.Y) pt2 = New Point(e.CellRect.Right, e.CellRect.Y) Dim linGrBrush As System.Drawing.Drawing2D.LinearGradientBrush linGrBrush = New System.Drawing.Drawing2D.LinearGradientBrush(pt1, pt2, Color.Blue, Color.Red) Dim rt As RectangleF rt = New RectangleF(e.CellRect.X, e.CellRect.Y, e.CellRect.Width, e.CellRect.Height) ' Fill the cell rectangle with the gradient. e.Graphics.FillRectangle(linGrBrush, e.CellRect) Dim whiteBR As Brush whiteBR = New SolidBrush(Color.White) Dim dispCol As C1.Win.C1TrueDBGrid.C1DisplayColumn dispCol = Me.C1TrueDBGrid1.Splits(0).DisplayColumns(e.Col) ' Center the text horizontally. Dim sfmt As New StringFormat() sfmt.Alignment = StringAlignment.Center ' Draw the text. e.Graphics.DrawString(dispCol.DataColumn.CellText(e.Row), dispCol.Style.Font, whiteBR, rt, sfmt) whiteBR.Dispose() ' Let the grid know the event was handled. e.Handled = True End If End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void C1TrueDBGrid1_OwnerDrawCell(object sender, C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs e) { if ( e.Col = 0 ) { // Create a gradient brush, blue to red. Point pt1, pt2; pt1 = new Point[e.CellRect.X, e.CellRect.Y]; pt2 = new Point[e.CellRect.Right, e.CellRect.Y]; System.Drawing.Drawing2D.LinearGradientBrush linGrBrush; linGrBrush = new System.Drawing.Drawing2D.LinearGradientBrush(pt1, pt2, Color.Blue, Color.Red); RectangleF rt; rt = new RectangleF(e.CellRect.X, e.CellRect.Y, e.CellRect.Width, e.CellRect.Height); // Fill the cell rectangle with the gradient. e.Graphics.FillRectangle(linGrBrush, e.CellRect); Brush whiteBR; whiteBR = new SolidBrush(Color.White); C1.Win.C1TrueDBGrid.C1DisplayColumn dispCol; dispCol = this.c1TrueDBGrid1.Splits[0].DisplayColumns[e.Col]; // Center the text horizontally. StringFormat sfmt = new StringFormat(); sfmt.Alignment = StringAlignment.Center; // Draw the text. e.Graphics.DrawString(dispCol.DataColumn.CellText[e.Row], dispCol.Style.Font, whiteBR, rt, sfmt); whiteBR.Dispose(); // Let the grid know the event was handled. e.Handled = true; } } |
There are a couple key points worth noting in this example: