ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Applying Cell Styles by Custom Criteria

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:

·      Visual Basic

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

·      C#

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;

    }

}

·      Delphi

procedure TWinForm.C1TrueDBGrid1_FetchCellStyle(sender: System.Object, e: C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs);

var

  N: Integer;

begin

  N := Int32.Parse(Self.C1TrueDBGrid1.Columns[e.Col].CellText(e.Row));

  if N > 1000 then

    e.CellStyle.ForeColor := System.Drawing.Color.Blue;

end;

The Split, Row, and Col properties identify which cell the grid is displaying. The CellStyle property conveys formatting information from the application to the grid. Since the CellStyle property is a Style object, a cell's font characteristics can also be changed in the FetchCellStyle event:

·      Visual Basic

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

·      C#

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;

}

·      Delphi

If N > 1000 Then

  e.CellStyle.Font.Italic := True;

 

var myfont: Font;

myfont := Font.Create(e.CellStyle.Font, FontStyle.Italic);

if N > 1000 then

  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:

·      Make the cell text red in column 4 if column 1 minus column 2 is negative.

·      Make the cell text bold in column 7 if it matches the contents of a text box.

In this case, set the FetchStyle property to True for columns 4 and 7, and handle the FetchCellStyle event as follows:

·      Visual Basic

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

·      C#

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);

    }

}

·      Delphi

procedure TWinForm.C1TrueDBGrid1_FetchCellStyle(sender: System.Object; e:  C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs);

var

  Col1, Col2: LongInt;

  S: String;

begin

  case e.Col of

  4:  begin

        Col1 := LongInt(Self.C1TrueDBGrid1.Columns[1].CellText(e.Row));

        Col2 := LongInt(Self.C1TrueDBGrid1.Columns[2].CellText(e.Row));

        if Col1 - Col2 < 0 then

          CellStyle.ForeColor := System.Drawing.Color.Red;

      end;

  7:  begin

        S := Self.C1TrueDBGrid1.Columns[7].CellText(e.Row);

        if S := TextBox1.Text then

        begin

          Dim myfont := New Font(CellStyle.Font, FontStyle.Bold);

          CellStyle.Font := myfont;

        end;

      end

  else

    Debug.WriteLine ('FetchCellStyle not handled: ');

  end;

end;

For efficiency reasons, only set FetchStyle to True for columns that you plan to handle in the FetchCellStyle event.

Note: The preceding examples use the CellText method for simplicity. However, the CellText and CellValue methods always create and destroy an internal clone of the dataset each time they are called, which may make them too inefficient to use in the FetchCellStyle event. To improve the performance of the grid's display cycle, try an unbound application. Unbound applications can access the underlying data source directly, which is generally faster than calling CellText or CellValue.

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:

·      Visual Basic

Private Sub TDBGrid1_FetchRowStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs) Handles C1TrueDBGrid1.FetchRowStyle

·      C#

private void TDBGrid1_FetchRowStyle( object sender, C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs e)

·      Delphi

procedure TDBGrid1_FetchRowStyle(sender: System.Object; e: C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs);

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.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.