Home > Blockchain >  Conversion from string "" to type 'Decimal' is not valid and Public member '
Conversion from string "" to type 'Decimal' is not valid and Public member '

Time:03-21

I found an error at the time of double click in cell gridview because there is a blank or empty record in the DTE column and qty column. Is there the best solution or recommendation? note : I use 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

Conversion from string "" to type 'Decimal' is not valid. Public member 'date' on type 'DBNull' not found. form gridview Conversion from string "" to type 'Decimal' is not valid.

CodePudding user response:

Note: This is an untested code. You need to check first if the columns DTE and QTY are not blank before assigning values to the controls.

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
  • Related