提问者:小点点

启动获取错误“运算符'='未为类型DBNULL和字符串定义


我最近开始得到这个错误,我不知道为什么。没有什么新的变化,我真的需要一些帮助

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

匿名用户

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(""

CStr("" & DBNull.Value)  ' ""
CStr("" & Nothing)       ' ""
CStr("" & "Hello World") ' "Hello World"