单元格值的条件格式

发布于 2024-11-02 06:53:00 字数 667 浏览 3 评论 0原文

我一直在研究 GridView 的条件格式,但我是 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 时为绿色,如果 <0 则为红色。

我在 int CellValue = 行上得到了不正确的格式。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听你说爱我 2024-11-09 06:53:01

尝试用以下

int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Difference"));

参考替换您的 int CellValue = 行:
http://www. johnchapman.name/asp-net-c-change-gridview-cell-background-color-based-on-value/

http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx

Try replacing your int CellValue = line with below one

int CellValue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Difference"));

Ref:
http://www.johnchapman.name/asp-net-c-change-gridview-cell-background-color-based-on-value/

http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx

眼睛会笑 2024-11-09 06:53:01

我会使用 int.TryParse 而不是 Convert.ToInt32,并验证您的文本实际上是数字。如果看起来正确,则可能是文本包含空格。

由于负数的格式如下($1,000.00)。检查字符串中是否存在括号,您可以根据该括号设置颜色格式

if (e.Row.Cells[2].Text.Contains(")")) {
  e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
} else {
  e.Row.Cells[2].BackColor = System.Drawing.Color.Green;
}

或更好

e.Row.Cells[2].BackColor = e.Row.Cells[2].Text.Contains(")") ? System.Drawing.Color.Red : System.Drawing.Color.Green;

I would use int.TryParse instead of Convert.ToInt32, and verify that your text is actually numeric. If it looks correct, a likely candidate is that the text contains spaces.

Since your negative numbers are formatted like so ($1,000.00). Check your string for the existance of parenthesis, and you can format the color based on that

if (e.Row.Cells[2].Text.Contains(")")) {
  e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
} else {
  e.Row.Cells[2].BackColor = System.Drawing.Color.Green;
}

or better yet

e.Row.Cells[2].BackColor = e.Row.Cells[2].Text.Contains(")") ? System.Drawing.Color.Red : System.Drawing.Color.Green;
剩一世无双 2024-11-09 06:53:01

您的值中有无效字符。把它们去掉,你就可以开始了

int CellValue = Convert.ToInt32(e.Row.Cells[2].Text.Replace("$","").Replace(",","").Replace(".",""));

可能有更好的方法,但现在尝试一下

编辑:要么更新我上面的更改,要么使用 double.Parse()

编辑:

int CellValue = (e.Row.Cells[2].Text.IndexOf('(') > -1) ? 0 : -1;

如果你使用 bool 会更好

bool CellValue = e.Row.Cells[2].Text.IndexOf('(') > -1;

You have invalid charactors in your values. Strip them out and you are good to go

int CellValue = Convert.ToInt32(e.Row.Cells[2].Text.Replace("$","").Replace(",","").Replace(".",""));

There might be a better way, but try that for now

Edit: Either update to my changes above or use double.Parse()

Edit:

int CellValue = (e.Row.Cells[2].Text.IndexOf('(') > -1) ? 0 : -1;

Better if you used a bool

bool CellValue = e.Row.Cells[2].Text.IndexOf('(') > -1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文