Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi.
When I insert a subtotal in the datagridview the code recalculates the grand total changes
my loop. count everything
I need your help or your ideas please.

What I have tried:

VB
Private Sub BtnSum_Click(sender As Object, e As EventArgs) Handles BtnSum.Click
    Dim somme As Double
    For x As Integer = 0 To DgvDevisClent.Rows.Count - 1
        somme += CType(DgvDevisClent.Rows(x).Cells(4).Value, Double)
    Next
    somme = String.Format("{0:##,##0.000}", CDbl(somme.ToString))
    DgvDevisClent.Rows.Insert(DgvDevisClent.CurrentRow.Index + 1, 1)
    DgvDevisClent.CurrentCell = DgvDevisClent.Rows(DgvDevisClent.CurrentRow.Index + 1).Cells(0)
    DgvDevisClent.CurrentRow.Cells(4).Value = somme
    DgvDevisClent.Focus()
End Sub
Posted
Comments
Richard MacCutchan 11-Dec-23 5:04am    
Assuming this code is in response to a button click, there is nothing there that will recalculate it after the total is added in. Maybe you need a flag in the row that holds the total so you always exclude that when summing the values.
HichSkill 11-Dec-23 6:07am    
When I refresh the code with a datagridview event the grand total changes in my label
example
first-item $1
second-item $2
Subtotal $3
My label displays $6 or place to display $3
Richard MacCutchan 11-Dec-23 10:26am    
See my response below.

You have the following line in your code:
VB
somme = String.Format("{0:##,##0.000}", CDbl(somme.ToString))

So, you convert the double value to a string, so that you can then convert it back to a double, in order to use it in the call to String.Format, which is a bit pointless. But you then use that string to set the value of somme, which is declared as a double. All of which makes no sense, so remove that line of code.
 
Share this answer
 
Comments
Richard Deeming 11-Dec-23 10:34am    
Technically, combined with VB's "evil type coercion", that's an extremely long-winded and inefficient way of writing somme = Math.Round(somme, 3) :)
Richard MacCutchan 11-Dec-23 11:05am    
I wondered why the compiler accepted it.
Maciej Los 12-Dec-23 16:29pm    
5ed!
Based on your comment -
Quote:
When I refresh the code with a datagridview event the grand total changes in my label
example
first-item $1
second-item $2
Subtotal $3
My label displays $6 or place to display $3

You are not clearing the original total to show the refreshed total when you refresh your datagrid -

VB
'Clear your label of all totals...
YourLabel.Text = ""

'Recalculate the total and update your label with the correct total...
Dim somme As Double
For x As Integer = 0 To DgvDevisClent.Rows.Count - 1
    somme += CType(DgvDevisClent.Rows(x).Cells(4).Value, Double)
Next
somme = String.Format("{0:##,##0.000}", CDbl(somme.ToString))

'Display the updated total in your label, PAY ATTENTION to the above solution and comments though...
YourLabel.Text = somme
 
Share this answer
 
Comments
Maciej Los 12-Dec-23 16:29pm    
5ed!
I'd change only one part: instead of CType(), i'd use DirectCast(). Why? Check out this: DirectCast Operator - Visual Basic | Microsoft Learn[^]
Another way is to get sum of cells in column 4 is to use Linq:

VB.NET
Dim some As Double = DgvDevisClent.Rows.Cast(Of DataGridViewRow) _
    .Select(Function(x) x.Cells(4).Value) _
    .Sum(Function(x) x)


As to me, you don't need to convert some value into string. This is strongly recommend to store values of the same type.
If you want to display value in a specific format, you should use DataGridViewColumn.DefaultCellStyle.Format property.

VB.NET
DgvDevisClent.Columns(4).DefaultCellStyle.Format = "N3" 'or N2


For further details, please see:
Intro to LINQ - Visual Basic | Microsoft Learn[^]
Format Data in DataGridView Control - Windows Forms .NET Framework | Microsoft Learn[^]
 
Share this answer
 
Comments
HichSkill 13-Dec-23 9:51am    
Thank you for your answer I tested the code but it does not work
Maciej Los 21-Dec-23 16:00pm    
"does not work" is not very descriptive...

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