I’ve been researching conditional formatting for GridViews all over the place, but I am new to ASP.Net and having a hard time. This is the code that I’ve found that makes the most sense to me:
protected void GridviewRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CellValue = Convert.ToInt32(e.Row.Cells[2].Text);
if (CellValue >= 0)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Green;
}
if (CellValue < 0)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
The GridView is incredibly simple: a header row and three columns with one row under the header with a currency amount in each column. I just need the data cell on that second row, third column to be green if >=0 and red if <0.
I am getting an incorrect format on the int CellValue = line.
Try replacing your
int CellValue =line with below oneRef:
http://www.johnchapman.name/asp-net-c-change-gridview-cell-background-color-based-on-value/
http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx