这是我的代码:
For Each row As DataGridViewRow In DataGridView1.Rows
Dim value = Val(row.Cells(9).Value)
If value = "no" Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
我一直收到这个错误:
System.InvalidCastException: 'Conversion from string "no" to type 'Double' is not valid.'
有什么想法吗?
提前致谢
答案 0 :(得分:3)
请勿使用Val
,因为这会将您的价值转换为双倍。使用ToString
将其转换为字符串,然后将其与" no"进行比较
For Each row As DataGridViewRow In DataGridView1.Rows
If row IsNot Nothing _
AndAlso row.Cells IsNot Nothing _
AndAlso row.Cells(9) IsNot Nothing Then
If If(row.Cells(9).Value, "").ToString().ToUpper() = "NO" Then
row.DefaultCellStyle.BackColor = Color.Red
ElseIf row.Cells(9).Value.ToString().ToUpper() = "YES" Then
row.DefaultCellStyle.BackColor = Color.Green
End If
End If
Next