[英]how to check not nullable column s in datagridview in vb.net
我有一個數據網格視圖,如果特定的列不為null,我想工作一些代碼。所以我給出了這樣的代碼:
For i As Integer = 0 To gv.RowCount - 2
If gv.Rows(i).Cells(1).Value IsNot System.DBNull.Value Then
Dim cnt As Integer = RecordPresent("CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells(1).Value)
If cnt = 0 Then
sqlInsertT1 = "Insert Into CompanyMaster_tbl(CompanyName) Values ('" + gv.Rows(i).Cells(1).Value + "')"
Exetransaction(sqlInsertT1)
Ccid = RecordID("Cid", "CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells(1).Value)
Else
Ccid = RecordID("Cid", "CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells(1).Value)
End If
End If
sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + gv.Rows(i).Cells(3).Value + "','" + gv.Rows(i).Cells(4).Value + "','" + gv.Rows(i).Cells(5).Value + "'," & Ccid & ");"
Exetransaction(sqlInsertT2)
Next
但是有時如果條件允許執行此條件內編寫的代碼,則此特定列也為null 如果gv.Rows(i).Cells(1).Value IsNot System.DBNull.Value然后
請嘗試使用IsDBNull(Expression)
代替,並檢查其是否為空字符串。 像這樣 :
If Not IsDBNull(gv.Rows(i).Cells(1).Value) AndAlso gv.Rows(i).Cells(1).Value.ToString.Length <> 0 Then
' YourCode
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.