Formatting Rows and Cells Meeting Specific Criteria
While changing styles will change the general appearance of the grid, you may want to change the grid's appearance based on conditions. 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 number of the row 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:
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
• C#
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;
}
}
This code changes the background color of the third row to red.
You can 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:
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
• C#
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 that you may need to change the column index in the above code. This code changes the font color of the cell consisting of the text "Chang" to green.
What You've Accomplished
In this topic you learned how to change the color of a specific row or a cell's font using the RowDataBound event. You can now further customize the appearance of the grid based on specific criteria.
|