?
Solved

Deleting rows from a datagrid

Posted on 2006-05-31
6
Medium Priority
?
232 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:Gary Samuels
  • 3
  • 2
6 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1200 total points
ID: 16802929
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
 

Author Comment

by:Gary Samuels
ID: 16803897
Already tried that, didn't work. The rowstate doesn't change until after the rowDelete procedure is completely finished.
0
 

Author Comment

by:Gary Samuels
ID: 16804048
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:vb_jonas
ID: 16805847
If (Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Detached) And (Not DataSet11.Details.Rows(iLoop).RowState = DataRowState.Deleted)
0
 
LVL 12

Accepted Solution

by:
vb_jonas earned 300 total points
ID: 16806608
Thing is the rowstate is deleted not detached. Tested and it worked fine for me.
0
 

Author Comment

by:Gary Samuels
ID: 16809862
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question