Deleting rows from a datagrid

VB.NET 2003 Windows Form

In a datagrid I'm trying to keep a subtotal of one field as uptodate as possible. In the code below I'm using the CurrentCellChanged to display the subtotal in a label. This is working good, except when a row is deleted. Does anyone have a suggestion on how to update the subtotal as soon as a row is deleted.

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.OleDbDataAdapter1.Fill(Me.DataSet11, "Details")
    End Sub

    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
        Try
            If DataSet11.Details.Count <> 0 Then
                Label1.Text = GroupSubTotal().ToString("C")
            Else
                Label1.Text = "$0.00"
            End If
        Catch err As Exception
            'MessageBox.Show(err.Message, "CurrentCellChanged")
        End Try
    End Sub

    Private Function GroupSubTotal() As Decimal
        Dim SubTotal As Decimal = 0
        Dim max As Integer = DataSet11.Details.Count - 1
        Dim iLoop As Integer

        For iLoop = 0 To max
            If Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Detached Then
                SubTotal += CDec(DataSet11.Details.Rows(iLoop).Item("productPrice"))
            End If
        Next
        Return SubTotal
    End Function

End Class
Gary SamuelsPlant ManagerAsked:
Who is Participating?
 
vb_jonasConnect With a Mentor Commented:
Thing is the rowstate is deleted not detached. Tested and it worked fine for me.
0
 
Bob LearnedConnect With a Mentor Commented:
Add an event handler for the RowDeleted to the DataTable:

   AddHandler DataSet11.Tables(0).RowDeleted, AddressOf OnRowDeleted

   Private Sub OnRowDeleted(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)

   End Sub

Bob
0
 
Gary SamuelsPlant ManagerAuthor Commented:
Already tried that, didn't work. The rowstate doesn't change until after the rowDelete procedure is completely finished.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Gary SamuelsPlant ManagerAuthor Commented:
I guess I should elaborate a little more. The onRowDelete procedure calls the GroupSubTotal function. As I step through the function I can see the selected row, and the rowstate at this point has not changed so the "productPrice" is added to the SubTotal and the results remain the same as before the onRowDelete procedure was fired.


 Private Function GroupSubTotal() As Decimal

        Dim SubTotal As Decimal = 0
        Dim max As Integer = DataSet11.Details.Count - 1
        Dim iLoop As Integer

        For iLoop = 0 To max

            If Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Detached Then
                Debug.Write(iLoop & ":")
                Debug.WriteLine(DataSet11.Details.Rows(iLoop).RowState)
                SubTotal += CDec(DataSet11.Details.Rows(iLoop).Item("productPrice"))
            Else
                Debug.Write(iLoop & ":")
                Debug.WriteLine(DataSet11.Details.Rows(iLoop).RowState)
            End If
        Next

        Return SubTotal

    End Function

   
    Private Sub onRowDelete(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)

        Try
            If DataSet11.Details.Count <> 0 Then
                Label1.Text = GroupSubTotal().ToString("C")
            Else
                Label1.Text = "$0.00"
            End If

        Catch err As Exception
            MessageBox.Show(err.Message, "onRowDelete")
        End Try

    End Sub


0
 
vb_jonasCommented:
If (Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Detached) And (Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Deleted)
0
 
Gary SamuelsPlant ManagerAuthor Commented:
I'm so confused, so what else is new? I should have known not to question TheLearnedOne. It's working correctly now. I don’t even have to test for the rowstate. As soon as I hit the delete key the row is gone as far as the dataset is concerned.  I call the GroupSubTotal function from the onRowDelete procedure and the subtotal is recalculated correctly. And thanks to vb_jonas your input made me go back and watch the rowstate even closer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.