c# - 单元格值的条件格式

我一直在到处研究 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/

相关文章:

latex - 如何在 LaTeX 中的空格后设置制表位?

java - 格式字符串变为 xxx1、xx10 或 1###、10## 等

python - 避免在字符串中指定相同值的最pythonic方法是什么

wpf - 使用小数分隔符格式化 XAML 中的值?

iphone - 时间跨度的智能格式化

c# - 使用 .NET 格式化大数

formatting - 在 Redmine wiki 页面中插入特殊字符

php - 正则表达式 - 去除非数字并删除美分(如果有)

c# - 为什么只有一个语句的方法需要大括号?

android - 如何在 Android 上将持续时间格式化为具有本地化时间单位的字符串?