How to delete items from a DataTable during an enumeration


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'm trying to enumerate all the rows in a DataTable and if a certain condition is met, delete the current row. .NET doesn't like this:





Dim myTable As DataTable

'...fill table, two columns a string and an integer

For Each myRow As DataRow In myTable.Rows

If myRow.Item(1) = 34 Then

myTable.Delete(myRow)

End If

Next





It bombs when the Next statement hits. Is there another way to do this?

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
It is because you are changing the contents of the value being enumerated.  You can't do this.  Just create an array or list that has all of the rows that you want to delete inside of the loop.  Then create another loop that cycles through the array you created to actually delete the rows.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Thanks for the help. While implementing what you suggestion, I noticed that the DataRow has a Delete method...then I noticed an AcceptChanges property on the DataTable. I tried this and it works:





Dim myTable As DataTable

'...fill table, two columns a string and an integer

For Each myRow As DataRow In myTable.Rows

If myRow.Item(1) = 34 Then

myRow.Delete()

End If

Next

myTable.AcceptChanges







Apparently, the Delete() method of the row just marks it for deletion (thus doesn't whack the enumeration). The AcceptChanges property actually removes the rows.
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
This has nothing to do with data tables, but rather any type of collection in .NET.  If you had tried removing items from a collection inside of a loop you would have had the same issue.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Got it. I think the DataRow.Delete() works because it doesn't remove the item from the collection until you AcceptChanges, which I'm doing after the enum.
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Good. Smile
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search