单元格值的条件格式
我一直在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试用以下
参考替换您的
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 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
我会使用 int.TryParse 而不是 Convert.ToInt32,并验证您的文本实际上是数字。如果看起来正确,则可能是文本包含空格。
由于负数的格式如下($1,000.00)。检查字符串中是否存在括号,您可以根据该括号设置颜色格式
或更好
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
or better yet
您的值中有无效字符。把它们去掉,你就可以开始了
可能有更好的方法,但现在尝试一下
编辑:要么更新我上面的更改,要么使用 double.Parse()
编辑:
如果你使用 bool 会更好
You have invalid charactors in your values. Strip them out and you are good to go
There might be a better way, but try that for now
Edit: Either update to my changes above or use double.Parse()
Edit:
Better if you used a bool