How to show a running total column of monthly datagridview amounts

Software: vb.net & sql server 2005
I have a datagridview showing monthly amounts that I want to add a Year to Date Column:
Sample code would be appreciated...

Example:
  Month        Amount   Year to Date
| Jan-2010 | 1000.00 |       1000.00 |
| Feb-2010 | 1000.00 |       2000.00 |

Thanks!
dave_skyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nepaluzCommented:
Handle the CellValueChanged event, eg

Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

    'this array contains the indexes of columns NOT including the Year to Date
    If {0, 1}.Contains(e.ColumnIndex) Then
        'note the columns are 1 and 2 to fit your example, change to suit your actual needs
        Dim tValue = Me.DataGridView1.Item(0, e.RowIndex).Value + Me.DataGridView1.Item(1, e.RowIndex).Value
        Me.DataGridView1.Item(2, e.RowIndex).Value = tValue
    End If
End Sub

You can always add your data-validation on the values BEFORE attempting to add them!
CodeCruiserCommented:
How do you populate the DGV? using a stored procedure? Show us the query.
dave_skyAuthor Commented:
CodeCruiser:

My knowledge is limited on some of the basic terminology.

Would Query be: Binding source with table adapter?

I am not using a stored procedure.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

dave_skyAuthor Commented:
nepaluz:

Would you mind extending your previous code for additional months?

Month        Amount   Year to Date
| Jan-2010 | 1000.00 |       1000.00 |
| Feb-2010 | 1000.00 |       2000.00 |
| Mar-2010 | 1000.00 |       3000.00 |
| Apr-2010 | 1000.00 |       4000.00 |

Also:
On {0, 1} of your previous code, I am getting an error message of an "Expression expected". I have tried all the changes that I know of with no luck.

Thanks
dave_skyAuthor Commented:
CodeCruiser:

I am using drag and drop from the dataset wizard. Not writing all of the code...
nepaluzCommented:
Not sure why you aregetting the error, my guess is you are using and "older" .net, try:

If {0, 1}.Contains(e.ColumnIndex) =True Then


The code should work for all months, its only limited by the columns, which in your case are three.
nepaluzCommented:
You'll need to have your dataGridView allow user to add new rows

    Dim tValue = Nothing
    If {1}.Contains(e.ColumnIndex) Then
        For x = 0 to Me.DataGridView1.RowCount - 1
           tValue = Me.DataGridView1.Item(1, x).Value + tValue
        Next
        Me.DataGridView1.Item(1, Me.DataGridView1.RowCount - 1
).Value = tValue
    End If
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.