2016-11-21 56 views
-1

每次代碼加載時,我都在處理代碼中的一列,並檢查DatagridView中第三列的值;如果是空的,它顯示「空」,但是當它具有內在價值,它表明「不空」檢查DataGridView中的列值是否爲空

林本守則

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    Try 
     For each row as DataGridViewRow in DataGridView1.Rows 
       If row.Cells(2).Value is Nothing Then 
        MsgBox("Empty") 
       Else 
        MsgBox("Not Empty") 
       EndIf 
     Next 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

End Sub 

,但我得到不正確的數據的工作,我有價值觀從DataGridView

-------------------------- 
    |NAME | AGE | TIME IN | 
-------------------------- 
    PAUL 18   

但它示出了

"Not Empty" 

在所述第一消息和

"Empty" 

第二條消息,但我只想顯示一條消息說「空」,因爲第一行的第三列不包含任何值。

+0

然後用'退出For'你提示你的消息停止循環後! –

+0

你的例子在datagrid中有一行,所以它應該只顯示一條消息,並立即退出循環。或者你沒有向我們展示整個示例數據。 –

+0

那麼修復了這個問題:)退出For循環:) –

回答

0

這解決我的問題:)謝謝

Try 
     For each row as DataGridViewRow in DataGridView1.Rows 
       If row.Cells(2).Value is Nothing Then 
        MsgBox("Empty") 
        Exit For 
       Else 
        MsgBox("Not Empty") 
        Exit For 
       EndIf 
     Next 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 
+1

只是FYI,如果你只檢查一行,則不需要For循環。只要使用If DataGridView1.Rows(0).Cells(2).Value Is Nothing ...等等 – Whitey