简体   繁体   中英

vb.net Conditional Formatting Gridview Cells

Here is my code that conditionally formats a specific Gridview cell (The value is a string that I convert to decimal)

 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

The problem is Decimals have no Null value

So I want the cell backcolor to be White when it's empty (as in no number), currently it shows as Firebrick because it classes 0 the same as nothing/null

Any ideas on how to achieve this?

Decimal.TryParse() should have a return value if the parse was successfull

try:

'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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM