dearness
asked on
How to use a variables datatype as a condition .
I am trying to tidy up data in a datagridview that is populated from a SQL server database, I want to change any cells containing "1/1/1900 12:00:00 AM" and 0 to an empty cell, or "" .
I have no problem with the "1/1/1900 12:00:00 AM" string but the 0 is single datatype. The datagridview contains several different datatypes so the loop needs to work for all of them.
See attached loop.
I have no problem with the "1/1/1900 12:00:00 AM" string but the 0 is single datatype. The datagridview contains several different datatypes so the loop needs to work for all of them.
See attached loop.
For Each dgvr As DataGridViewRow In dgvCooks.Rows
For Each dgvc As DataGridViewCell In dgvr.Cells
'this does not work because value is SINGLE data type
'Conversion from string "20-CBL3" to type 'Double' is not valid.
If Trim(dgvc.Value) = 0 Then
dgvc.Value = ""
End If
'This converts time string to empty string - Ok
If dgvc.Value.ToString = "1/1/1900 12:00:00 AM" Then
dgvc.Value = ""
End If
Next
Next
handle the DataError event of the datagridview and that default error will "go away". re-run the code and see what you get.
PS> Handling the DataError event simply involves adding this code to the form class, e.g for DataGridView1:
PS> Handling the DataError event simply involves adding this code to the form class, e.g for DataGridView1:
Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
End Sub
ASKER
Ok, that got rid of the error but the cells are still showing 0, i need blank.
step through your code and see what this evaluates to when the cell is 0
Dim xTest = dgvc.Value.Tostring
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
xTest = 0
Cells shows 0.0 due to .DefaultCellStyle.Format = "N1"
Cells shows 0.0 due to .DefaultCellStyle.Format = "N1"
see my last comment
Open in new window