Link to home
Start Free TrialLog in
Avatar of dearness
dearnessFlag for American Samoa

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.
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

Open in new window

Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

If Trim(dgvc.Value.Tostring) = "0" Then

Open in new window

Avatar of dearness

ASKER

Nepaluz,
Thanks for the response, I get an exception error when the input value is 0
User generated image
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:
Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError

End Sub

Open in new window

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
xTest = 0
Cells shows 0.0  due to .DefaultCellStyle.Format = "N1"
see my last comment