• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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

0
dearness
Asked:
dearness
  • 5
  • 3
1 Solution
 
nepaluzCommented:
If Trim(dgvc.Value.Tostring) = "0" Then

Open in new window

0
 
dearnessAuthor Commented:
Nepaluz,
Thanks for the response, I get an exception error when the input value is 0
Exception error
0
 
nepaluzCommented:
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dearnessAuthor Commented:
Ok, that got rid of the error but the cells are still showing 0, i need blank.
0
 
nepaluzCommented:
step through your code and see what this evaluates to when the cell is 0
Dim xTest = dgvc.Value.Tostring

Open in new window

0
 
nepaluzCommented:
PS. If thedatatype is set to 0 AND there is a default value for the column then you can not achieve what you want to do.
If the 0 to "" formating is simply for display (easy reading), then just change the fore-color of the cell to the cell's back color to " hide" the 0
e.g
dgvc.Style.ForeColor=color.white

Open in new window

0
 
dearnessAuthor Commented:
xTest = 0
Cells shows 0.0  due to .DefaultCellStyle.Format = "N1"
0
 
nepaluzCommented:
see my last comment
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now