vb.net - 开始出现错误“未为 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
解决方案
您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"
推荐阅读
- sql - 从字符串中删除前导破折号
- android - 尝试在 Android JSON 中的空对象引用上调用虚拟方法“int java.lang.String.length()”
- html - JavaScript 提交表单未正确传递隐藏值
- postman - Postman pre-request script iterate over request body JSON
- javascript - 找到最小的 - Codewars 挑战 - Javascript
- css - 为什么按钮元素不受 CSS 影响?
- android - BindingAdapter 中带有对象属性枚举的问题
- javascript - 如何在本机反应中隐藏特定屏幕上的底部导航栏?
- javascript - 在没有模块加载器的情况下在 TypeScript 中使用 ES2015 模块
- r - 如何获取R中数据框中所有变量的唯一值的计数