Pages

Friday, November 11, 2011

ASP.NET C# Change GridView Cell Background Color Based on Value


GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  int value = (int)DataBinder.Eval(e.Row.DataItem, e.Row.Cells[2].Text);
  // e.Row.Cells[2] references the cell value you want to use
  if (value < 100)
  {
   e.Row.Cells[2].BackColor = Color.FromName("#c6efce");
  }
  if ((value >= 100) && (value < 500))
  {
   e.Row.Cells[2].BackColor = Color.FromName("#ffeb9c");
  }
  if (value >= 500)
  {
   e.Row.Cells[2].BackColor = Color.FromName("#ffc7ce");
  }
 }
}

No comments:

Post a Comment