Deleting multiple rows
-
I'm working on a shoppingCart and try to delete multiple rows at once, but get error message: "Collection was modified; enumeration operation may not execute." Here is my code: Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click For Each row As GridViewRow In GridView1.Rows Dim chk As CheckBox = CType(row.FindControl("Remove"), CheckBox) If Not chk Is Nothing AndAlso chk.Checked Then Dim ID = New Guid(GridView1.DataKeys(row.RowIndex).Value.ToString()) StoreManager.RemoveItem(ID) End If Next End Sub Code for: Class StoreManager Public Shared Sub RemoveItem(ByVal id As Guid) ShoppingCart.Remove(id) End Sub Code for: Class ShoppingCart Public Sub Remove(ByVal id As Guid) For Each existingProd As OrderedProducts In _items If id = existingProd.ID Then _items.Remove(existingProd) End If Next 'error calls this line!!! End Sub Not sure how to fix error!
-
I'm working on a shoppingCart and try to delete multiple rows at once, but get error message: "Collection was modified; enumeration operation may not execute." Here is my code: Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click For Each row As GridViewRow In GridView1.Rows Dim chk As CheckBox = CType(row.FindControl("Remove"), CheckBox) If Not chk Is Nothing AndAlso chk.Checked Then Dim ID = New Guid(GridView1.DataKeys(row.RowIndex).Value.ToString()) StoreManager.RemoveItem(ID) End If Next End Sub Code for: Class StoreManager Public Shared Sub RemoveItem(ByVal id As Guid) ShoppingCart.Remove(id) End Sub Code for: Class ShoppingCart Public Sub Remove(ByVal id As Guid) For Each existingProd As OrderedProducts In _items If id = existingProd.ID Then _items.Remove(existingProd) End If Next 'error calls this line!!! End Sub Not sure how to fix error!
You cannot use For/Each/Next on a collection if you're going to modify the collection. You have to use For/Next with an indexing variable, and you have to iterate over the collection backwards, meaning you start at the end and work your way back to the beginning.
For i As Integer = GridView1.Rows.Length - 1 to 0 Step -1 . . Next
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
You cannot use For/Each/Next on a collection if you're going to modify the collection. You have to use For/Next with an indexing variable, and you have to iterate over the collection backwards, meaning you start at the end and work your way back to the beginning.
For i As Integer = GridView1.Rows.Length - 1 to 0 Step -1 . . Next
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Hmmm, I tried this but still get same error For i As Integer = GridView1.Rows.Count - 1 To 0 Step -1 Dim row As GridViewRow = GridView1.Rows(i) Dim chk As CheckBox = CType(row.FindControl("Remove"), CheckBox) If Not chk Is Nothing AndAlso chk.Checked Then Dim ID = New Guid(GridView1.DataKeys(row.RowIndex).Value.ToString()) StoreManager.RemoveItem(ID) End If Next
-
Hmmm, I tried this but still get same error For i As Integer = GridView1.Rows.Count - 1 To 0 Step -1 Dim row As GridViewRow = GridView1.Rows(i) Dim chk As CheckBox = CType(row.FindControl("Remove"), CheckBox) If Not chk Is Nothing AndAlso chk.Checked Then Dim ID = New Guid(GridView1.DataKeys(row.RowIndex).Value.ToString()) StoreManager.RemoveItem(ID) End If Next
Ok, found the problem and fixed it: Public Sub Remove(ByVal id As Guid) For i As Integer = _items.Count - 1 To 0 Step -1 Dim existingProd As OrderedProducts = _items(i) If id = existingProd.ID Then MsgBox("Found Product! Count: " & _items.Count.ToString() & "items") _items.Remove(existingProd) MsgBox("Deleted Product! Count: " & _items.Count.ToString() & "items") End If Next End Sub Thanks a lot!!!