Change GridView BackColor

The code sample is written in C#. It handles the DataBound event for a GridView control. When the event occurs, the row color of the GridView control is changed according to a Label in the page.


protected void GridView1_DataBound(object sender, EventArgs e)
{
double Gridrow = GridView1.Rows.Count;
for (int i = 0; i < Gridrow; i++)
{
GridViewRow row;
row = GridView1.Rows[i];

string Cplace;
Cplace = ((Label)row.Cells[2].FindControl("Label3")).Text;

switch (Cplace)
{
case "Pali":
row.BackColor = System.Drawing.Color.Cyan;
break;

case "Colcatta":
row.BackColor = System.Drawing.Color.SkyBlue;
break;

case "Goa":
row.BackColor = System.Drawing.Color.Salmon;
break;

case "Delhi":
row.BackColor = System.Drawing.Color.SeaGreen;
break;

case "Chennai":
row.BackColor = System.Drawing.Color.Snow;
break;
}
}

}


Comments

No responses found. Be the first to comment...