Solved

Datagridview column running total

Posted on 2013-12-24
5
652 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

15 Experts available now in Live!

Get 1:1 Help Now