如何在vb.net中检查datagridview中不可为空的列
我有一个数据网格视图,如果特定列不为null,我想编写一些代码。因此,我给出了如下代码:如何在vb.net中检查datagridview中不可为空的列,vb.net,datagridview,Vb.net,Datagridview,我有一个数据网格视图,如果特定列不为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.Row
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不是System.DBNull.Value,则尝试改用IsDBNull(表达式)
,并检查它是否为空字符串。像这样:
If Not IsDBNull(gv.Rows(i).Cells(1).Value) AndAlso gv.Rows(i).Cells(1).Value.ToString.Length <> 0 Then
' YourCode
End If
如果不是IsDBNull(gv.Rows(i).Cells(1).Value)和also gv.Rows(i).Cells(1).Value.ToString.Length 0,则
“你的代码
如果结束
欢迎您的光临。很乐意帮忙。