Dynamically Change GridView cell background color based on condition in ASP.Net C#

In this tutorial we have learn how to Dynamically Change GridView cell background color based on condition in ASP.Net C#,The GridView Cell’s background color will be dynamically changed based on its value in the RowDataBound event of GridView.

The Gridview has 4 columns total. The 4th column contains Rank of a color e.g. “Yellow”, Green”, “Marron”, etc. and depending on the rank value color of that cells’ back color should change accordance to rank column

First,Create Gridview RowDatabound Event “GridView1_RowDataBound” and apply apply logic to TableCell Value for change background color Dynamically.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int rank = int.Parse(e.Row.Cells[4].Text);
if (rank < 20)
{
e.Row.Cells[4].BackColor = Color.Yellow;
}
else if (rank < 40)
{
e.Row.Cells[4].BackColor = Color.Maroon;
}
else
{
e.Row.Cells[4].BackColor = Color.Green;
}
}
}

Dynamically Change GridView cell background color based on condition in ASP.Net C#

SEE MORE

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *