Change cell color on different values – Gridview

This is called Conditional Formatting

You can enable the RowDataBound Event in the markup

<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">

</asp:GridView>

And put this in your Code-Behind file.

protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(e.Row.RowIndex == 0)     // This is row no.1
            if(e.Row.Cells[0].Text == "ABC")
                e.Row.Cells[0].BackColor = Color.Red;

        if(e.Row.RowIndex == 1)     // This is row no.2
            if(e.Row.Cells[0].Text == "CBA")
                e.Row.Cells[0].BackColor = Color.Green;
    }
}

Leave a Comment