Solved

# Datagridview column running total

Posted on 2013-12-24
704 Views
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!
0
Question by:dave_sky
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 13

Expert Comment

ID: 39738642
.
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
0

Author Comment

ID: 39738695
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
``````

Can you help?
0

Author Comment

ID: 39740928
k-designers,

Any ideas on the error I am getting on your sample code?
0

LVL 13

Accepted Solution

Jesus Rodriguez earned 500 total points
ID: 39741260
Let me clarify the answer before. On This code

``````Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click

Dim tblbreakeven As New DataTable
Dim C1 as New DataColum
With C1
.ColumnName="COL1'
.Datatype=System.getType("Sysytem.Double")
End With
Dim C2 as New DataColum
With C2
.ColumnName="COL2'
.Datatype=System.getType("Sysytem.Double")
End With

'Add the rows values for COL1 and then

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
``````

I was assuming  that the tblbreakeven dtatable was containing the values on the col1 already inside.

But if you already has the grid Binding to a datatable or has the grid with the datasource you can do this

``````         Dim Sum as Double=0
For Each rw As DataGridViewRow In DataGridView1.Rows
Sum += rw.Cells(0).Value
rw.cells(1).value=Sum
Next

``````
0

Author Closing Comment

ID: 39742431
Thanks!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

VB.net VSTO Excel Ribbon error 4 28
CSS question 16 63
vb.net application has warrnings about VB6 calls. 2 48
Background worker passing messagebox yes no 13 26
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
###### Suggested Courses
Course of the Month4 days, 6 hours left to enroll