vb.net条件格式Gridview单元格

时间:2016-10-25 14:20:53

标签: asp.net vb.net gridview

这是我的代码,它有条件地格式化特定的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

问题是小数没有空值

所以我希望单元格背面颜色为白色时为空(如无数字),目前它显示为Firebrick,因为它将0归类为无/ null

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

1 个答案:

答案 0 :(得分:0)

如果解析成功,

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