ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Task-Based Help / Setting the Grid's Appearance / Formatting Rows by Specific Criteria
In This Topic
    Formatting Rows by Specific Criteria
    In This Topic

    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. br/>In Code
      Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.FetchRowStyles = True
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.FetchRowStyles = true;
      
    2. Add the FetchRowStyle event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub C1TrueDBGrid1_FetchRowStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs) Handles C1TrueDBGrid1.FetchRowStyle
       
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void c1TrueDBGrid1_FetchRowStyle(object sender, C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs e)
      {
       
      }
      
    3. Declare the variables to get the values in the Birth and Death columns by adding the following code to the FetchRowStyle event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       ' 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
      

      To write code in C#

      C#
      Copy Code
      // 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;
      
    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:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' 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
      

      To write code in C#

      C#
      Copy Code
      // 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;
          }
      
    5. Highlight the rows that contain an empty cell by adding the following code after the code in step 4:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' 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
      

      To write code in C#

      C#
      Copy Code
      // If the Birth or Death column contains an empty cell, highlight the row.
          if (bday == "" || ddate == ""
          {
              e.CellStyle.BackColor = Color.PaleGreen;
          }
      

    What You've Accomplished

    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.