David Svedarsky
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!
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!
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
Can you help?
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
Can you help?
ASKER
k-designers,
Any ideas on the error I am getting on your sample code?
Any ideas on the error I am getting on your sample code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Dim Sum as Double=0
For I as Integer=0 to Tbl.Rows.Count-1
Sum+=Tbl.Rows(I).Item("COL
Tbl.Rows(I).BeginEdit
Tbl.Rows(I).Item("COL2")=S
Tbl.Rows(I).EndEdit()
Tbl.AcceptChanges()
Next I
YourGrid.Datasource=Tbl