For cases where regular expressions are insufficient to express formatting requirements, use the FetchCellStyle event to customize fonts and colors on a per-cell basis. This event will only be fired for columns that have the FetchStyle property set to True.
For example, provide color coding for values that fall within a certain range. The following code assumes that the FetchStyle property is True for a single column of numeric data, and handles the FetchCellStyle event to display values greater than 1000 in blue:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TrueDBGrid1_FetchCellStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs) Handles C1TrueDBGrid1.FetchCellSTyle Dim N As Integer N = Val(Me.C1TrueDBGrid1(e.Row, e.Col) If N > 1000 Then e.CellStyle.ForeColor = System.Drawing.Color.Blue End If End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void c1TrueDBGrid1_FetchCellStyle( object sender, C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs e) { int N; N = (int) this.c1TrueDBGrid1[e.Row, e.Col]; if ( N > 1000 ) { e.CellStyle.ForeColor = System.Drawing.Color.Blue; } } |
The Split, Row, and Colproperties identify which cell the grid is displaying. The CellStyleproperty conveys formatting information from the application to the grid. Since the CellStyleproperty is a Style object, a cell's font characteristics can also be changed in the FetchCellStyle event:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
If N > 1000 Then e.CellStyle.Font.Italic = True Dim myfont As Font myfont = New Font (e.CellStyle.Font, FontStyle.Italic) If N > 1000 Then e.CellStyle.Font = myfont |
To write code in C#
C# |
Copy Code
|
---|---|
if ( N > 1000 ) { e.CellStyle.Font.Italic = true } Font myfont; myfont = new Font (e.CellStyle.Font, FontStyle.Italic); if ( N > 1000 ) { e.CellStyle.Font = myfont; } |
The FetchCellStyle event can also be used to apply formatting to one cell based upon the values of other cells, or even other controls. For example, suppose that you want to:
In this case, set the FetchStyle property to True for columns 4 and 7, and handle the FetchCellStyle event as follows:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TrueDBGrid1_FetchCellStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs) Handles C1TrueDBGrid1.FetchCellStyle Select Case e.Col Case 4 Dim Col1 As Long, Col2 As Long Col1 = CLng(Me.C1TrueDBGrid1(e.Row, 1)) Col2 = CLng(Me.C1TrueDBGrid1(e.Row, 2)) If Col1 - Col2 < 0 Then CellStyle.ForeColor = System.Drawing.Color.Red Case 7 Dim S As String S = Me.C1TrueDBGrid1(e.Row, 7).ToString() If S = TextBox1.Text Then Dim myfont = New Font(CellStyle.Font, FontStyle.Bold) CellStyle.Font = myfont End If Case Else Debug.WriteLine ("FetchCellStyle not handled: " & e.Col) End Select End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void c1TrueDBGrid1_FetchCellStyle( object sender, C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs e) { switch (e.Col) { case 4: long Col1, long Col2; Col1 = (long)this.c1TrueDBGrid1[e.Row, 1]; Col2 = (long)this.c1TrueDBGrid1[e.Row, 2]; if ( Col1 - Col2 < 0 ) CellStyle.ForeColor = System.Drawing.Color.Red break; case 7: string S; S = this.c1TrueDBGrid1[e.Row, 7].ToString(); if ( S == TextBox1.Text ) { Font myfont = new Font(CellStyle.Font, FontStyle.Bold); CellStyle.Font = myfont; } break; default: Console.WriteLine ("FetchCellStyle not handled: " + e.Col); } } |
For efficiency reasons, only set FetchStyle to True for columns that you plan to handle in the FetchCellStyle event.
To customize fonts and colors on a per-row instead of a per-cell basis, use the FetchRowStyle event, which will only be fired once per row for grids that have the FetchRowStyles property set to True. The syntax for this event is as follows:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub TDBGrid1_FetchRowStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs) Handles C1TrueDBGrid1.FetchRowStyle |
To write code in C#
C# |
Copy Code
|
---|---|
private void TDBGrid1_FetchRowStyle( object sender, C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs e) |
Although the FetchRowStyle event can be used to implement an alternating row color scheme, an easier and more efficient way to accomplish the same task would be to use the AlternatingRows property, together with the built-in EvenRow and OddRow styles.