Results 1 to 5 of 5

Thread: Deleting a datatable row while looping through it

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Deleting a datatable row while looping through it

    I am using the code below to loop through the rows of a datatable. If a certain condition is met I then delete the row from the datatable.
    This then causes a "System.InvalidOperationException: 'Collection was modified; enumeration operation might not execute.'" error.
    What is the proper way to do this, so that the loop processes all rows? Thanks.


    Code:
            For Each row As DataRow In DT.Rows
    
                'do some stuff here
     
                If someCondition = True Then
                    DT.Rows.Remove(row)
                End If
    
            Next

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Deleting a datatable row while looping through it

    I should think pass the datarows to a new datarow collection, delete the needed rows and rebind that collection, although to be fair I haven't used gridview for a while so maybe a better solution exists.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,262

    Re: Deleting a datatable row while looping through it

    See here: https://stackoverflow.com/questions/...-for-each-loop
    You cannot delete an object from a collection during enumeration. You cannot modify the collection at all. That will cause an error (Collection was modified; enumeration operation may not execute).
    If i have to change (especially delete) an item in a "List", i use the second approach in the SO-Link: Going backwards through the List
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting a datatable row while looping through it

    The other option that's not mentioned in that SO answer is to create a new list containing all the items first and enumerate that. That way, removing items from the original list will not affect the list being enumerated. This is the least efficient option but most lists will be relatively small and so it won;t really make a noticeable difference and the code will be more succinct. E.g.
    Code:
            For Each row As DataRow In DT.Rows.Cast(Of DataRow)().ToList()
    
                'do some stuff here
     
                If someCondition = True Then
                    DT.Rows.Remove(row)
                End If
    
            Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Deleting a datatable row while looping through it

    Why do I have the impression that you can remove from a list with linq? And I have done so in the past...Hmmm....
    Also this might be helpful: http://venkateswarlu.net/dot-net/rem...ist-using-linq
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width