Click here to Skip to main content
15,887,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
I had this problem in a datagridview row where i have 3 columns. Datagridview data is loaded from query in vb.net. Below is the scenario:

Column1 Column2 Column3
3 75 3
3 76 3
3 IP 0
3 NFE 0

If "Column2" cells contains "IP" or "NFE", "Column3" value is "0" BUT
If "Column2" cells contains "75" or "76", "Column3" value is "EQUAL" to "Column1"

I got this code where i used "For Each" loop;

BUT sad to note that returns me this ERROR;

"Conversion from string "IP" to type 'Double' is not valid."

Please help me with this problem thank you.



What I have tried:

Private Sub DGVGRADES_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGVGRADES.CellValueChanged
        For Each row As DataGridViewRow In DGVGRADES.Rows
            If row.Cells(4).Value >= 75 Then
                row.Cells(0).Value = row.Cells(3).Value
            ElseIf row.Cells(4).Value.ToString <= 3 Then
                row.Cells(0).Value = row.Cells(3).Value
            ElseIf row.Cells(4).Value < 75 Then
                row.Cells(0).Value = 0
            ElseIf row.Cells(4).Value = "IP" Then
                row.Cells(0).Value = 0
            End If
        Next
    End Sub
Posted
Updated 7-Jan-18 6:39am
Comments
A_Griffin 30-Dec-17 12:42pm    
Why do your Cells(X) in your code range from 0 to 4 when you only have 3 columns - so should be just from 0 to 2?

1 solution

An error message is quite obvious... This means that your DataGridViewColumn[^] accepts only numeric data, but your source data contains text data.
Yous have got 2 options:

  1. change datatable into linq query which will return proper data types
    See: Queries in LINQ to DataSet | Microsoft Docs[^]
  2. or
  3. use different DataGridView event to be able to handle non-numeric data
    See: DataGridView.RowPrePaint Event[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900