【问题标题】:vb.net Conditional Formatting Gridview Cellsvb.net 条件格式设置 Gridview 单元格
【发布时间】:2016-10-25 14:20:52
【问题描述】:

这是我有条件地格式化特定 Gridview 单元格的代码(值是我转换为十进制的字符串)

 If e.Row.Cells(0).Text = "Total" Then
        Dim c2 As Decimal
        Decimal.TryParse(e.Row.Cells(7).Text, c2)
        If c2 >= 0 And c2 <= 84 Then
            e.Row.Cells(7).BackColor = Drawing.Color.Firebrick
            e.Row.Cells(7).ForeColor = Drawing.Color.White
        End If
        If c2 >= 85 Then
            e.Row.Cells(7).BackColor = Drawing.Color.LimeGreen
            e.Row.Cells(7).ForeColor = Drawing.Color.Black
        End If
    End If

问题是小数没有 Null 值

所以我希望单元格背景颜色为空时为白色(如无数字),目前它显示为 Firebrick,因为它的类 0 与 nothing/null 相同

关于如何实现这一点的任何想法?

【问题讨论】:

  • 使用CellFormatting 事件DataGridView。同时检查DBNull 可能是您正在寻找的。​​span>

标签: asp.net vb.net gridview


【解决方案1】:

如果解析成功,Decimal.TryParse() 应该有一个返回值

尝试:

'Code is not tested
Dim c2 As Decimal

If Not Decimal.TryParse(e.Row.Cells(7).Text, c2) Then
    'No double value found
    'Color as you wish
Else If c2 >= 0 And c2 <= 84 Then
    e.Row.Cells(7).BackColor = Drawing.Color.Firebrick
    e.Row.Cells(7).ForeColor = Drawing.Color.White
Else If c2 >= 85 Then
    e.Row.Cells(7).BackColor = Drawing.Color.LimeGreen
    e.Row.Cells(7).ForeColor = Drawing.Color.Black
End If

【讨论】:

    猜你喜欢
    • 2012-05-22
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多