How to change the row color of a Gridview row

By Ken Fitzpatrick

The following is snippet of code from a RowDataBound event handler for a Gridview named Gridview1. To change the background color of a Gridview row, you need to add a handler for the RowDataBound event. This event is called for each row added to a datagrid whem it is being bound from the datasource. Use the condition "If e.Row.RowType = DataControlRowType.DataRow" to only trap when a data row is being written as opposed to a header row or footer row.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
   If e.Row.RowType = DataControlRowType.DataRow Then
    If <your condition> Then  'This condition is only needed to change the color of select rows.
     e.Row.BackColor = Drawing.Color.Red 'Or any other color you want
    End If
   End If
End Sub

How to change the row color of a Gridview row  (2024 Views)