Question Problem with running talley in data grid

threetime

Member
Joined
Mar 25, 2012
Messages
5
Programming Experience
1-3
My problem is I want the Cost column to display an overall total for whatever is in the column. I have filter in place in the form of search feature. Instead of a total, it is only showing the last number in the column and the number changes according to the search as desired, but only the last number.

Here is the code in my event handler:

VB.NET:
Private Sub InventoryBindingSource_PositionChanged(ByVal sender As  Object, ByVal e As System.EventArgs) Handles  InventoryBindingSource.PositionChanged,  InventoryBindingSource.CurrentChanged
        If Me.InventoryBindingSource.Position > -1 Then
            Dim total As Double = 0
            For Each column As DataRowView In Me.InventoryBindingSource.List
                total = column!Cost
            Next
            Me.TextBox9.Text = Format(total, "c")
        End If
    End Sub

Cost Total Problem.png

As you can see, the Total should be $3,040.01 and not $3,000.

Thanks for any advice.
 
You are assigning the field value to 'total' each time, thus replacing what's in there already. Think about it. This is just simple maths. How would you tally up the column if you had to do it manually? You do it the same way here.
 
You are assigning the field value to 'total' each time, thus replacing what's in there already. Think about it. This is just simple maths. How would you tally up the column if you had to do it manually? You do it the same way here.

Thanks jm, as soon as I started reading I could have smacked myself. Maybe too many hours looking at code.

Answer is

VB.NET:
total = total + column!Cost

Thanks again.
 
Back
Top