You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

出现错误:类型DBNULL与string的'='运算符未定义,求排查帮助

解决“operator '=' is not defined for type DBNULL and string”错误

Hey there, let's break down what's causing this error and fix it up. The issue here is that your code is trying to compare a DBNull value directly with the string "Departure"—and VB.NET doesn't support that operation. Even though you didn't touch your code, the problem is almost certainly new null data showing up in your DataGridView (maybe a database record got updated with a null value, or someone added a blank row that's being interpreted as DBNull).

Looking at your code snippet, the line triggering the error is this one:

If e.Value = "Departure" Then

When e.Value is DBNull.Value instead of a valid string, VB can't resolve the = operator between those two types—hence the confusing error message.

Fix 1: Check for DBNull before comparing

Here's a revised version of your code that handles null values safely:

' First verify e.Value isn't null before doing the string comparison
If Not IsDBNull(e.Value) Then
    If e.Value.ToString() = "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
    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
    End If
Else
    ' Optional: Handle the null case (set a default color, leave as-is, etc.)
    dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.White
    dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.White
    dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.White
End If

Fix 2: Simplified null-first flow

If you prefer a more linear structure, you can rearrange the code to handle nulls first:

If IsDBNull(e.Value) Then
    ' Handle null values first
    dgvNotify.Item(0, e.RowIndex).Style.BackColor = Color.White
    dgvNotify.Item(1, e.RowIndex).Style.BackColor = Color.White
    dgvNotify.Item(2, e.RowIndex).Style.BackColor = Color.White
ElseIf e.Value.ToString() = "Departure" Then
    ' Apply pink styling for Departure entries
    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
Else
    ' Apply green styling for all other valid values
    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
End If

Why this started happening now?

Since you didn't modify your code, the root cause is almost certainly a change in your data source:

  • A database column that previously didn't allow nulls was updated to permit them, and a new null record was added
  • A user entered a blank value in the relevant field, which gets passed to the grid as DBNull
  • Your data feed (like a CSV or API) started returning empty values that VB maps to DBNull

内容的提问来源于stack exchange,提问作者MarkHutch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:13:07