0

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.

roy
  • 693
  • 2
  • 11
  • Well not to be mean, but the error messages you're getting explain exactly the problem. The best way to fix is really up to you. You can test for invalid values before attempting to assign it to the controls, but IMHO you are one 100% better to prevent invalid data getting there in the first place – Hursey Mar 18 '22 at 03:43
  • `dim cellValue = DataGridView1.Rows(x).Cells(0).Value txtCODE.Text = If(cellValue Is Nothing OrElse cellValue Is DBNull.Value, String.Empty, cellValue.ToString())` -- You always need to check for null. -- If `DataGridView1.Rows(x).Cells(1).Value` is a DateTime, you need to cast that value to Date (using `DirectCast()`). And, you guessed it, you need to check for null. – Jimi Mar 18 '22 at 03:48
  • There's no reason to cast to string here: `NumericUpDown1.Value = DataGridView1.Rows(x).Cells(2).Value.ToString()`, cast to decimal instead (if that value is decimal). Set `Option Strict On` before anything else -- You should update your language version to a more recent one. The first assignment then becomes just `txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value?.ToString()` – Jimi Mar 18 '22 at 03:55
  • @Jimi , I've updated the code according to your recommendation but there was an error. `Conversion from string "" to type 'Decimal' is not valid.` . You can see from my update code and screenshots. `txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value?.ToString()` I can't use the operator "?" in my version vb.net .directcast you mean I don't understand yet. – roy Mar 18 '22 at 04:56
  • That's why I suggested to set `Option Strict` to `On` right away (possibly though Visual Studio's general settings, in relation to the VB.Net language). -- `DateTimePicker1.Value` is a DateTime, `NumericUpDown1.Value` is decimal (or what you have configured it to be, a numeric value in any case). Hence, you cannot assign a `[Value].ToString()`, neither accept a string as `Value`. You have to cast a Cell value to the correct Type. For `txtCODE.Text` is `String`, not for the others. – Jimi Mar 18 '22 at 05:37
  • @Jimi , I've set up Strict to On option and you better give you an answer – roy Mar 18 '22 at 06:25

1 Answers1

1

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
Kuro Neko
  • 795
  • 12
  • 19
  • 1
    `e.RowIndex` returns the current Row index -- See my comments. – Jimi Mar 18 '22 at 03:52
  • `Dim dt = DataGridView1.Rows(x).Cells(1).Value.date.ToString()` >> Public member 'date' on type 'DBNull' not found. I tried your code there is an error in the line code – roy Mar 18 '22 at 05:02
  • @Jack I edited my code and removed the `.date` – Kuro Neko Mar 18 '22 at 05:13
  • I've tried updating the code from you no error but there is still less because by the time I double click cell gridview should datetimepicker1 be blank instead of appear date now – roy Mar 18 '22 at 06:27
  • I've tried updating the code from you no error but there is still less because by the time I double click cell gridview should datetimepicker1 become blank according to cell gridview instead of appear date now – roy Mar 18 '22 at 06:33
  • @Jack You can refer to this [Set DateTimePicker value to be null](https://stackoverflow.com/questions/5947726/set-datetimepicker-value-to-be-null) if you want a blank DateTimePicker. Or you may try using textbox/label to set blank values easily. – Kuro Neko Mar 18 '22 at 07:38
  • 1
    @ TheCoolCat,, sorry I reply too late and thank you for your help and answer – roy Mar 21 '22 at 03:35