eltoroloco eltoroloco - 1 month ago 15
Vb.net Question

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?

Answer

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
Comments