Gary Samuels
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.DataSet 11, "Details")
End Sub
Private Sub DataGrid1_CurrentCellChang ed(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChang ed
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.Messa ge, "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(iLo op).RowSta te = DataRowState.Detached Then
SubTotal += CDec(DataSet11.Details.Row s(iLoop).I tem("produ ctPrice"))
End If
Next
Return SubTotal
End Function
End Class
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(
End Sub
Private Sub DataGrid1_CurrentCellChang
Try
If DataSet11.Details.Count <> 0 Then
Label1.Text = GroupSubTotal().ToString("
Else
Label1.Text = "$0.00"
End If
Catch err As Exception
'MessageBox.Show(err.Messa
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(iLo
SubTotal += CDec(DataSet11.Details.Row
End If
Next
Return SubTotal
End Function
End Class
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(iLo op).RowSta te = DataRowState.Detached Then
Debug.Write(iLoop & ":")
Debug.WriteLine(DataSet11. Details.Ro ws(iLoop). RowState)
SubTotal += CDec(DataSet11.Details.Row s(iLoop).I tem("produ ctPrice"))
Else
Debug.Write(iLoop & ":")
Debug.WriteLine(DataSet11. Details.Ro ws(iLoop). RowState)
End If
Next
Return SubTotal
End Function
Private Sub onRowDelete(ByVal sender As Object, ByVal e As System.Data.DataRowChangeE ventArgs)
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.Messag e, "onRowDelete")
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(iLo
Debug.Write(iLoop & ":")
Debug.WriteLine(DataSet11.
SubTotal += CDec(DataSet11.Details.Row
Else
Debug.Write(iLoop & ":")
Debug.WriteLine(DataSet11.
End If
Next
Return SubTotal
End Function
Private Sub onRowDelete(ByVal sender As Object, ByVal e As System.Data.DataRowChangeE
Try
If DataSet11.Details.Count <> 0 Then
Label1.Text = GroupSubTotal().ToString("
Else
Label1.Text = "$0.00"
End If
Catch err As Exception
MessageBox.Show(err.Messag
End Try
End Sub
If (Not DataSet11.Details.Rows(iLo op).RowSta te = DataRowState.Detached) And (Not DataSet11.Details.Rows(iLo op).RowSta te = DataRowState.Deleted)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER