繁体   English   中英

如果 datagridview 单元格不为空,那么? 别的?

[英]If datagridview cell isnot null then ? else?

在插入 SQL 时,我有可以为空的列机器 ID。 如果该字段中有某种类型,我希望它检查该值是否存在。 这工作得很好。 但是由于它不是必需的表条目,我希望它在为空时忽略该字段并插入空值。 现在我已经尝试了几种不同的方法,它要么继续验证字段,即使是空的,要么添加所有内容而不验证。 这是最新的尝试:

        If IsDBNull(DataGridView3.Rows(i).Cells("Machine ID").Value) = False Then

            STSQL = "select machine_id from mpcs.machine where machine_id = " & "'" & DataGridView3.Rows(i).Cells("Machine ID").Value & "'"
            rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)
            If Not rsMPCS.HasRows Then
                MessageBox.Show("Not a valid Machine ID")
                Return
            End If
            rsMPCS.Close()
        ElseIf IsDBNull(DataGridView3.Rows(i).Cells("Machine ID").Value) = True Then
            DataGridView3.Rows(i).Cells("Machine ID").Value = vbNull
        End If

好的,我发现这种方式有效。

        If MachID = Nothing Then
            MachID = vbNull
        Else
            STSQL = "select machine_id from mpcs.machine where machine_id = " & "'" & DataGridView3.Rows(i).Cells("Machine ID").Value & "'"
            rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)
            If Not rsMPCS.HasRows Then
                MessageBox.Show("Not a valid Machine ID")
                Return
            End If
            rsMPCS.Close()
        End If

如果我没记错的话应该是

DataGridView3.Rows(i).Cells("Machine ID").Value = nothing

而你的 if 子句是错误的。 正确的语法是If Convert.IsDBNull(...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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