我最近开始收到此错误,我不确定原因。没有什么新变化,我真的可以使用一些帮助
If e.Value = "Departure" Then
dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightPink
dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightPink
dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightPink
'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightPink
'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor
Else
dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightGreen
dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightGreen
dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightGreen
'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightGreen
'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor
End If
End If
End Sub
答案 0 :(得分:2)
您e.Value
似乎是NULL
所以您需要改进if
:
If CStr("" & e.Value) = "Departure" Then
dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightPink
dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightPink
dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightPink
'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightPink
'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor
Else
dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.LightGreen
dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.LightGreen
dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.LightGreen
'dgvNotify.DefaultCellStyle.SelectionBackColor = Color.LightGreen
'dgvNotify.DefaultCellStyle.SelectionForeColor = dgvNotify.DefaultCellStyle.ForeColor
End If
CStr("" & e.Value)
将您的e.Value
转换为字符串值:
CStr("" & DBNull.Value) ' ""
CStr("" & Nothing) ' ""
CStr("" & "Hello World") ' "Hello World"