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:
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; } } |
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; } } |
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.