Posts tagged row backcolor
Conditional Formatting in a GridView
Dec 17th
This code will allow you to change the background colour of a cell or entire row depending on certain conditions.
In this example we test if the row being bound is a data row, then if the second column (indexed as 1) is equal to 0. If it is then the row’s backcolor is set as PeachPuff and the cell itself has backcolor Salmon. Remember that cell indexing starts at 0 so that the first column is e.Row.Cells(0), the second column e.Row.Cells(1), etc.
Protected Sub grdTable_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdTable.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(1).Text = “0″ Then
e.Row.BackColor = System.Drawing.Color.PeachPuff
e.Row.Cells(1).BackColor = Drawing.Color.Salmon
End If
End If
End Sub
To change the text colour simply substitute BackColor with ForeColor.


