从字符串“”到类型 'Decimal' 的转换无效,并且 vb 中找不到类型 'DBNull' 上的 Public 成员 'date',net

Conversion from string "" to type 'Decimal' is not valid and Public member 'date' on type 'DBNull' not found in vb,net

我发现在cell gridview中双击时出现错误,因为DTE列和qty列中有空白或空记录。 有没有最好的解决方案或推荐? 注意:我使用 visual studio 2010

Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
Dim cellValue = DataGridView1.Rows(x).Cells(1).Value
        DateTimePicker1.Value = CDate(If(cellValue Is Nothing OrElse cellValue Is DBNull.Value, String.Empty, cellValue.ToString()))
        Dim cellValue1 = DataGridView1.Rows(x).Cells(2).Value
        NumericUpDown1.Value = CDec(If(cellValue1 Is Nothing OrElse cellValue1 Is DBNull.Value, String.Empty, cellValue1.ToString()))
End Sub

注意:这是未经测试的代码。在为控件赋值之前,您需要先检查列 DTE 和 QTY 是否不为空。

Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        x = e.RowIndex
        txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()

        Dim dt = DataGridView1.Rows(x).Cells(1).Value.ToString()
        Dim qty = DataGridView1.Rows(x).Cells(2).Value.ToString()

        ' Check if DTE column is not empty
        If dt <> nothing Then
            DateTimePicker1.Value = Cdate(dt).Date
        Else
            ' you can do something here
        End If
        
        ' Check if QTY column is a number
        If IsNumeric(qty) Then
            NumericUpDown1.Value = qty   
        Else
            NumericUpDown1.Value = 0
        End If
End Sub