Task-Based Help / Formatting the Grid's Content / Setting Conditional Formatting
In This Topic
Setting Conditional Formatting
In This Topic

In some cases you may want to change the grid's appearance based on given conditions.

In Code

You can change the appearance of grid rows and cells matching specific criteria using the RowDataBound event.

To change the color of a specific row or a cell's font using the RowDataBound event, complete the following steps:

  1. Specify the row number you want to change using the RowIndex property.
  2. Set the desired color of the C1GridViewRow.BackColor property.

    For example, add the following code to the RowDataBound event:

    To write the code in Visual Basic:

    Visual Basic
    Copy Code
    Protected Sub C1GridView1_RowDataBound(ByVal sender As Object, ByVal e As C1.Web.UI.Controls.C1GridView.C1GridViewRowEventArgs) Handles C1GridView1.RowDataBound
         If (e.Row.RowIndex = 2) Then
             e.Row.BackColor = System.Drawing.Color.Red
         End If
     End Sub

    To write the code in C#:

    Visual Basic
    Copy Code
    private void C1GridView1_RowDataBound(object sender, C1.Web.UI.Controls.C1GridView.C1GridViewRowEventArgs e)
          {
              if ((e.Row.RowIndex == 2)) {
              e.Row.BackColor = System.Drawing.Color.Red;
          }
    }
  3. You can also change the color of the font used in a specific cell by specifying the text in the cell and the desired color.

    For example, add the following code to the RowDataBound event:

    To write the code in Visual Basic:

      
    Visual Basic
    Copy Code
    Protected Sub C1GridView1_RowDataBound(ByVal sender As Object, ByVal e As C1.Web.UI.Controls.C1GridView.C1GridViewRowEventArgs) Handles C1GridView1.RowDataBound
         If (e.Row.Cells(0).Text = "Chang") Then
             e.Row.Cells(0).ForeColor = System.Drawing.Color.Green
         End If
     End Sub

    To write the code in C#:

      
    C#
    Copy Code
    private void C1GridView1_RowDataBound(object sender, C1.Web.UI.Controls.C1GridView.C1GridViewRowEventArgs e)
     {
         if ((e.Row.Cells[0].Text == "Chang")) {
            e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
         }
     }
    Note: You may need to change the column index in the code. This code changes the font color of the cell consisting of the text "Chang" to green.

What You've Accomplished

The first code snippet changes the background color of the third row to red.
 

The second code snippet changes the color of a specific row or a cell's font using the RowDataBound event.


See Also