开始获取错误“operator'='未定义类型DBNULL和字符串

问题描述 投票:0回答:1

我最近开始收到这个错误,我不知道为什么。没有什么新变化,我真的可以使用一些帮助

        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
1个回答
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"
© www.soinside.com 2019 - 2024. All rights reserved.