如何在vb.net的datagridview中检查不可为空的列

时间:2014-01-08 13:02:24

标签: 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.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那么

1 个答案:

答案 0 :(得分:1)

尝试使用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