Insert DGV1 Select Row in DGV2, decimal places are sometimes not displayed correctly

astonmartin

New member
Joined
Sep 12, 2024
Messages
4
Programming Experience
Beginner
Hello,

I select the row in DGV1 that I need and add it to DGV2,
in DGV1 I have the columns “Quantity”, “E-Price” and “Total”, in DGV2 I have the same columns.
What I came across is that when I changed the quantity in DGV1 from 1 piece to 2 pieces
a decimal place is missing in DGV2 column “Total”, this always happens when I enter an even number in DGV1
but not with odd numbers.
Example:
in DGV1 column “Quantity” is 1 piece, E-price 40,90 € = total 40,90 €, in DGV2 also the same.
If I now change the quantity in DGV1 to 2 pieces, €40.9 appears in DGV2 in the “Total” column.

DGV1 is bound, DGV2 is unbound

Here is the code how I add the row from DGV1 to DGV2:


VB.NET:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click


'Copy selected rows from Datagridview1 to Datagridview2
Datagridview2.ColumnCount = 5 'Number of columns
For Each dgvRow In DataGridView1.SelectedRows

Dim row3() As String = {dgvRow.Cells(1).Value.ToString(), dgvRow.Cells(6).Value.ToString(), dgvRow.Cells(3).Value.ToString(), dgvRow.Cells(7).Value.ToString() , dgvRow.Cells(7).Value.ToString * dgvRow.Cells(6).Value.ToString() }

Datagridview2.Rows.Add(row3)


Next
End Sub

Don't get the error...

Greetings Andi
 
If the first grid is bound then you should not be getting the data from the grid and converting it to Strings. You should be getting it from the data source and passing it as the actual type it is. The issue is likely that the data is being rounded during conversion. You need to put some actual thought into data types and not just assume that Strings everywhere is a good idea. This:
VB.NET:
dgvRow.Cells(7).Value.ToString * dgvRow.Cells(6).Value.ToString()
is a prime example of not thinking about data types. You presumably have two numbers to begin with. You then explicitly convert them both the Strings. You then try to multiply them, but Strings can't be multiplied, so they must be implicitly converted back to numbers, not necessarily of the same type as you started with. You then put the result into a String array, which means that numeric result must be implicitly converted to a String again. That's madness. Just get the numbers from the data source as numbers, multiply them and then put the number in the second grid as numbers, preferably via a data source.

If you need more help then you need to start by explaining EXACTLY what the data source is. If it's a DataTable, explain what it's schema is.
 
Back
Top