ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Formatting Rows By Specific Criteria

To format rows based on specific criteria, use the FetchRowStyles property and the FetchRowStyle event. In this example, rows that do not have values in the Birth or Death columns will be highlighted green and all other rows will be locked and formatted in Steel Blue, Tahoma font.

1.   Set the FetchRowStyles property to True.

In the Designer

Locate the FetchRowStyles property in the Properties window and set it to True.

In Code

Add the following code to the Form_Load event:

·      Visual Basic

Me.C1TrueDBGrid1.FetchRowStyles = True

·      C#

this.c1TrueDBGrid1.FetchRowStyles = true;

·      Delphi

Self.C1TrueDBGrid1.FetchRowStyles := True;

2.   Add the FetchRowStyle event:

·      Visual Basic

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

 

End Sub

·      C#

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

{

 

}

·      Delphi

procedure C1TrueDBGrid1_FetchRowStyle(sender: object; e: C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs);

 

end;

3.   Declare the variables to get the values in the Birth and Death columns by adding the following code to the FetchRowStyle event:

·      Visual Basic

    ' Declare variables to get the values in the columns.

    Dim bday As String = Me.C1TrueDBGrid1.Columns("Birth").CellText(e.Row).ToString

    Dim ddate As String = Me.C1TrueDBGrid1.Columns("Death").CellText(e.Row).ToString

·      C#

    // Declare variables to get the values in the columns.

    string bday = this.c1TrueDBGrid1.Columns["Birth"].CellText(e.Row).ToString;

    string ddate = this.c1TrueDBGrid1.Columns["Death"].CellText(e.Row).ToString;

·      Delphi

var

  bday : String;

  ddate : String;

begin

  // Declare variables to get the values in the columns.

  bday := Self.C1TrueDBGrid1.Columns['Birth'].CellText(e.Row).ToString;

  ddate := Self.C1TrueDBGrid1.Columns['Death'].CellText(e.Row).ToString;

4.   Disable editing and change the font if there is an empty cell in either the Birth or Death column by adding the following code after the code in step 3:

·      Visual Basic

    ' If the Birth or Death column does not contain an empty cell, disable editing and change the font.

    If (bday <> "" AndAlso ddate <> "") And (bday <> "" OrElse ddate <> "") Then

        e.CellStyle.Locked = True

        e.CellStyle.Font = New Font("Tahoma", 9)

        e.CellStyle.ForeColor = Color.SteelBlue

    End If

·      C#

    // If the Birth or Death column does not contain an empty cell, disable editing and change the font.

    if ((bday != "" && ddate != "") And (bday != "" || ddate != ""))

    {

        e.CellStyle.Locked = true;

        e.CellStyle.Font = new Font("Tahoma", 9);

        e.CellStyle.ForeColor = Color.SteelBlue;

    }

·      Delphi

  // If the Birth or Death column does not contain an empty cell, disable editing and change the font.

  if (((bday <> '') and (ddate <> '')) And ((bday <> '') or (ddate <> ''))) then

  begin

    e.CellStyle.Locked := True;

    e.CellStyle.Font := Font.Create('Tahoma', 9);

    e.CellStyle.ForeColor := Color.SteelBlue;

  end;

5.   Highlight the rows that contain an empty cell by adding the following code after the code in step 4:

·      Visual Basic

    ' If the Birth or Death column contains an empty cell, highlight the row.

    If bday = "" Or ddate = "" Then

        e.CellStyle.BackColor = Color.PaleGreen

    End If

·      C#

    // If the Birth or Death column contains an empty cell, highlight the row.

    if (bday == "" || ddate == ""

    {

        e.CellStyle.BackColor = Color.PaleGreen;

    }

·      Delphi

  // If the Birth or Death column contains an empty cell, highlight the row.

  if ((bday = '') or (ddate = '')) then

    e.CellStyle.BackColor := Color.PaleGreen;

This topic illustrates the following:

Rows with blank values in the Birth or Death column are highlighted and all other rows are not editable and in a different font. Adding a value to a blank cell will change the formatting of the cell.


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