Solved

Datagridview column running total

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but it's slow 28 70
What can cause the styling on a .NET site to not be found? 17 25
How do you do a one to many list in .NET CORE? 2 26
No Data for DropDown List 2 24
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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