How to show a running total of monthly datagridview amounts in textbox

Software: vb.net & sql server 2005

I have a datagridview showing monthly amounts in a column that I want to see totaled in a Year To Date textbox on the main form
Sample code would be appreciated...

Example:
  Month        Amount  
| Jan-2010 | 1000.00 |  
YTDTextBox value = 1000.00

| Feb-2010 | 1000.00 |
YTDTextBox value = 2000.00

| Mar-2010 | 1000.00 |
YTDTextBox value = 3000.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
    Dim tValue = Nothing
    If {1}.Contains(e.ColumnIndex) Then
        For x = 0 Me.DataGridView1.RowCount-1
           tValue = Me.DataGridView1.Item(0, e.RowIndex).Value + tValue
        Next
        YTDTextBox.Text = tValue
    End If
End Sub
nepaluzCommented:
Sorry, contained  errors. Amend as:


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
    Dim tValue = Nothing
    If {1}.Contains(e.ColumnIndex) Then
        For x = 0 Me.DataGridView1.RowCount-1
           tValue = Me.DataGridView1.Item(1, x).Value + tValue
        Next
        YTDTextBox.Text = tValue
    End If
End Sub
dave_skyAuthor Commented:
Shows a Syntax error on:
 "Me" of "For x = 0 Me.TblBreakEvenDataGridView.RowCount-1"
No other errors...

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

        'this array contains the indexes of columns NOT including the Year to Date
        Dim tValue = Nothing
        Dim x = Nothing

        If e.ColumnIndex = 0 Then
        For x = 0 Me.TblBreakEvenDataGridView.RowCount-1
                tValue = Me.TblBreakEvenDataGridView.Item(1, x).Value + tValue
            Next
            TextBoxYTD.Text = tValue
        End If

    End Sub
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

dave_skyAuthor Commented:
Sorry, should have been

 If e.ColumnIndex = 1 Then
        For x = 0 Me.TblBreakEvenDataGridView.RowCount-1
                tValue = Me.TblBreakEvenDataGridView.Item(1, x).Value + tValue
nepaluzCommented:
Private Sub TblBreakEvenDataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblBreakEvenDataGridView.CellValueChanged

        Dim tValue As Double = 0.0
        If e.ColumnIndex = 1 Then
        For x = 0 to Me.TblBreakEvenDataGridView.RowCount-1
                tValue = CDbl(Me.TblBreakEvenDataGridView.Item(1, x).Value) + tValue
            Next
            TextBoxYTD.Text = tValue
        End If

    End Sub

Also, from your data, the columnindex SHOULD be 1 not 0  so I have changed it (change it back if you wish, and hopefully, the IDE won't be so pedantic)

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
Mike EghtebasDatabase and Application DeveloperCommented:
You can also dothis at query level:        
Select T.Date, T.Amount, (Select Sum(Amount) From Table1 
Where T.Date >= Table1.Date) 'YTD' From Table1 T
Order By T.Date

Open in new window

Mike EghtebasDatabase and Application DeveloperCommented:
Format the date in SELECT portion like MMM YYYY but keep it as shown below in order by clause:

Order By T.Date
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.