Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Deleting multiple rows

Deleting multiple rows

Scheduled Pinned Locked Moved Visual Basic
helptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TAK78
    wrote on last edited by
    #1

    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!

    D 1 Reply Last reply
    0
    • T TAK78

      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!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        T Offline
        T Offline
        TAK78
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • T TAK78

          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

          T Offline
          T Offline
          TAK78
          wrote on last edited by
          #4

          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!!!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups