Tuesday, July 17, 2007

Change Gridview row color

One way to change the entire row color of a GridView is to do it during RowDataBound(). If a flag returns true from the database I wanted to make that entire row red. Here is how I did that:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView test = (System.Data.DataRowView)e.Row.DataItem;
bool actionPlanExists = SqlConvert.ToBoolean(test.Row[“ActionPlanExists”]);
if (actionPlanExists == true)
{
e.Row.BackColor = System.Drawing.Color.Red;
e.Row.ForeColor = System.Drawing.Color.White;
}
}
}