Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datagridview column running total

Posted on 2013-12-24
5
Medium Priority
?
770 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
[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
  • Learn & ask questions
  • 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 2000 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

1
 

Author Closing Comment

by:dave_sky
ID: 39742431
Thanks!
0

Featured Post

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

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)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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