简体   繁体   English

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

[英]how to check not nullable column s in datagridview in vb.net

I have a data grid view i want to work some code if particular column is not null..so i given code like this: 我有一个数据网格视图,如果特定的列不为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

but some time this particular column null also if condition allow to execute the code written inside this condition If gv.Rows(i).Cells(1).Value IsNot System.DBNull.Value Then 但是有时如果条件允许执行此条件内编写的代码,则此特定列也为null 如果gv.Rows(i).Cells(1).Value IsNot System.DBNull.Value然后

try use IsDBNull(Expression) instead, And Check if it's empty string or not. 请尝试使用IsDBNull(Expression)代替,并检查其是否为空字符串。 Like this : 像这样 :

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM