首页 > 解决方案 > 开始出现错误“未为 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

标签: vb.net

解决方案


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"

推荐阅读