Link to home
Start Free TrialLog in
Avatar of Gary Samuels
Gary SamuelsFlag for United States of America

asked on

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
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary Samuels

ASKER

Already tried that, didn't work. The rowstate doesn't change until after the rowDelete procedure is completely finished.
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


If (Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Detached) And (Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Deleted)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.