Avatar of dave_sky
dave_sky
Flag for United States of America asked on

Datagridview column running total

I need sample code for the following example:

Col1      Col2

1. 123|  123
2. 123|  246
3. 123|  369
4. 123|  492

I want to keep a running total of Col1 values in Col2.
Row 1 Col2 to show Col1 cell 1 value
Row 2 Col2 to show sum of Col1 cells 1 & 2.
Row 3 Col2 to show sum of Col2 cells 1 & 2 & 3
Row 4 Col2 to show sum of Col2 cells 1 & 2 & 3 & 4

Column 1 is databound - Column 2 can be databound or unbound...

Thanks!
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
dave_sky

8/22/2022 - Mon
Jesus Rodriguez

.
Dim Sum as Double=0
For I as Integer=0 to Tbl.Rows.Count-1
  Sum+=Tbl.Rows(I).Item("COL1")
   Tbl.Rows(I).BeginEdit
   Tbl.Rows(I).Item("COL2")=Sum
   Tbl.Rows(I).EndEdit()
   Tbl.AcceptChanges()
Next I

YourGrid.Datasource=Tbl
dave_sky

ASKER
I tried this and get an "Object reference not set to an instance of an object".error at TblBreakeven in: For I As Integer = 0 To TblBreakEven.Rows.Count - 1
Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click

        Dim tblbreakeven As DataTable
        Dim Sum As Double = 0
        For I As Integer = 0 To TblBreakEven.Rows.Count - 1
            Sum += tblbreakeven.Rows(I).Item("COL1")
            TblBreakEven.Rows(I).BeginEdit()
            TblBreakEven.Rows(I).Item("COL2") = Sum
            TblBreakEven.Rows(I).EndEdit()
            TblBreakEven.AcceptChanges()
        Next I

        TblBreakEvenDataGridView.DataSource = TblBreakEven
end Sub

Open in new window


Can you help?
dave_sky

ASKER
k-designers,

Any ideas on the error I am getting on your sample code?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Jesus Rodriguez

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dave_sky

ASKER
Thanks!