我一直在到处研究 GridViews 的条件格式,但我是 ASP.Net 的新手并且遇到了困难。这是我发现对我最有意义的代码:
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;
}
}
}
GridView 非常简单:一个标题行和三列,标题下有一行,每列有货币金额。我只需要第二行第三列的数据单元格在 >=0 时为绿色,在
我的 int CellValue = 行格式不正确。
最佳答案
尝试将您的 int CellValue =
行替换为以下一行
int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Difference"));
引用: http://www.johnchapman.name/asp-net-c-change-gridview-cell-background-color-based-on-value/
http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx
https://stackoverflow.com/questions/5707719/