簡體   English   中英

如何檢查Datagridview中的Cell是否具有值並繼續VB.Net

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

嗨,我的代碼試圖

  • 第一:查找值是否在第0列“ CODE”中

  • 第二:如果找到,則檢查列1“ STATUS”的值是否為“ IN”。如果為true,則顯示MsgBox,並顯示消息“ Ticket Used”。

這是我的代碼:

    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使用的票證。 如果單元格沒有值,它將鎖定並顯示錯誤:

未為類型“ DBNull”和字符串“ IN”定義System.InvalidCastException:Operator'='
在main.vb:line366

有什么幫助嗎?

在進行比較之前添加DbNull檢查:

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

閱讀以下線程以獲取更多處理DbNull數據的選項: 在vb.net中處理dbnull數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM