开始出现错误"运营商' ='没有为类型DBNULL和字符串定义

时间:2018-05-17 14:20:41

标签: vb.net

我最近开始收到此错误,我不确定原因。没有什么新变化,我真的可以使用一些帮助

        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

1 个答案:

答案 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"