Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a datagridview and i want to transfer a decimal value from one column cell to another. the second cell is also decimal type.
when i try to run i receive a message saying that "is not a valid value for decimal" or "commit error". Please help me.

What I have tried:

here is my code.

        Dim p As Integer = 0
        Do While p <= DataGridView1.RowCount - 1
            For q As Integer = 3 To 2 Step -1
                If DataGridView1.Rows(p).Cells(1).Value <> "rep" Then


                    If DataGridView1.Rows(p).Cells(q).Value IsNot Nothing Then
                        'DataGridView1.Rows(p).Cells(q).GetType.Name

                        Dim column_type = (DataGridView1.Rows(p).Cells(q)).ValueType
                       Dim column_type2 = (DataGridView1.Rows(p).Cells(q + 2)).ValueType

                        If column_type Is GetType(String) Then
                            MsgBox("ok")

                        Else
                            MsgBox(column_type.ToString & "mmmm")
                            MsgBox(column_type2.ToString & "nnn")
                            MsgBox(DataGridView1.Rows(p).Cells(q).Value)
                            MsgBox(DataGridView1.Rows(p).Cells(q + 2).Value & "hhh")
                        End If

        DataGridView1.Rows(p).Cells(q + 2).Value = DataGridView1.Rows(p).Cells(q).Value
                        DataGridView1.Rows(p).Cells(q).Value = ""
                    End If
                End If

            Next
            p = p + 1
        Loop
Posted
Updated 18-Mar-21 22:08pm

1 solution

If both cells (columns) are the same type, then "transfer" can be made as simple as follow:

VB.NET
DataGridView1.Rows(p).Cells(q+2)) = DataGridView1.Rows(p).Cells(q))
 
Share this answer
 
Comments
Ralf Meier 19-Mar-21 9:04am    
There is no real difference to the code from the OP. It must work with .Value as well ...
I suppose that one (or more) of the source-Cells doesn't exist ...
Maciej Los 19-Mar-21 16:46pm    
It may be much more...
Datagridview can be bound with datasource, which is read only..., etc.
Member 14904307 21-Mar-21 14:05pm    
yes, dear Maciej Los , the datagridview is bound to datasource and datagridview is read only. As you see the code i posted is similar as your's, but i don't understand the source of problem
Member 14904307 21-Mar-21 15:46pm    
Dear Maciej Los , i finally change DataGridView1.Rows(p).Cells(q).Value = "" by DataGridView1.Rows(p).Cells(q).Value = 0 and it works. Apparently it was a problem of data type. Thanks.
Maciej Los 21-Mar-21 16:13pm    
You're very welcome.

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