Solved

Datagridview column running total

Posted on 2013-12-24
5
638 Views
Last Modified: 2013-12-27
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
Comment
Question by:dave_sky
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
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

by:dave_sky
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

Open in new window


Can you help?
0
 

Author Comment

by:dave_sky
ID: 39740928
k-designers,

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

Accepted Solution

by:
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
        tblbreakeven.Add(C1)
        tblbreakeven.Add(C2)

        '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 

Open in new window


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
 

Open in new window

0
 

Author Closing Comment

by:dave_sky
ID: 39742431
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now