【发布时间】:2011-08-08 03:22:55
【问题描述】:
我一直在到处研究 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 = 行格式不正确。
【问题讨论】:
-
这确实看起来不错,发生了什么,它不起作用?
-
e.Row.Cells[2].Text 的值是多少?它是空的吗? Row.Cells[2] 存在吗?它是空的吗?由于那是您遇到问题的那条线,请分解那条线。
-
使用 Convert.ToInt32() 时,如果值为 null,则返回零。
-
您能否提供正在引发的异常的消息。
-
它提供错误 FormatException is unhandled by user code。输入字符串的格式不正确。那里有一个价值。就是同一个gridview的第一列和第二列的区别
标签: c# gridview formatting conditional