如何检查 Datagridview 中的单元格是否有值并继续 VB.Net

How to check if Cell In Datagridview has a value and continue VB.Net

嗨,我的代码正在尝试

这是我的代码:

    Private Sub changefound()

    Dim findtxt As String = txt_Find.Text
    Try
        If DataGridView2.Rows.Count > 0 Then
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                ' For j As Integer = 0 To DataGridView2.Columns.Count - 1 'Not using this because we only want to look in CODE column.
                Dim CellChange As String = DataGridView2.Rows(i).Cells("CODE").Value.ToString

                If CellChange.Contains(findtxt) = True Then
                    If DataGridView2.Rows(i).Cells("STATUS").Value = "IN" Then    'This is Line 366
                        MsgBox("Tickets USed")
                        Exit Sub


                    Else
                        With DataGridView2
                            ' .Rows(i).Cells(j).Style.BackColor = Color.Gray
                            '  .Rows(i).Cells(j).Value = findtxt
                            ' MsgBox(i & j + 1)

                            .Rows(i).Cells("STATUS").Value = "IN"


                            Exit Sub
                        End With
                    End If
                End If
                    'Next   'This is the Jfor 
            Next
        End If
    Catch e As Exception
        MessageBox.Show(e.ToString())
    End Try


End Sub

我以任何方式移动它,它只会检查是否找到 IN,MSGBox 是否会使用票证。 如果单元格没有值,它会锁定并出现错误:

System.InvalidCastException:Operator'=' is not defined for type"DBNull" and string "IN"
At main.vb:line366

有什么帮助吗?

在进行比较之前添加 DbNull 检查:

.....
If Not IsDbNull(DataGridView2.Rows(i).Cells("STATUS").Value) _
        AndAlso DataGridView2.Rows(i).Cells("STATUS").Value = "IN" Then 
.....

阅读以下线程以获取更多处理 DbNull 数据的选项: handling dbnull data in vb.net