﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » How to delete items from a DataTable during an enumeration</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Tue, 09 Jun 2026 07:24:20 GMT</lastBuildDate><ttl>20</ttl><item><title>How to delete items from a DataTable during an enumeration</title><link>http://forum.strataframe.net/FindPost6314.aspx</link><description>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:&lt;br&gt;
&lt;br&gt;
[codesnippet]&lt;br&gt;
Dim myTable As DataTable&lt;br&gt;
'...fill table, two columns a string and an integer&lt;br&gt;
For Each myRow As DataRow In myTable.Rows&lt;br&gt;
  If myRow.Item(1) = 34 Then&lt;br&gt;
    myTable.Delete(myRow)&lt;br&gt;
  End If&lt;br&gt;
Next&lt;br&gt;
[/codesnippet]&lt;br&gt;
&lt;br&gt;
It bombs when the Next statement hits.  Is there another way to do this?&lt;br&gt;</description><pubDate>Sat, 27 Jan 2007 10:27:43 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: How to delete items from a DataTable during an enumeration</title><link>http://forum.strataframe.net/FindPost6335.aspx</link><description>Good. :)</description><pubDate>Sat, 27 Jan 2007 10:27:43 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: How to delete items from a DataTable during an enumeration</title><link>http://forum.strataframe.net/FindPost6326.aspx</link><description>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.</description><pubDate>Fri, 26 Jan 2007 20:19:39 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: How to delete items from a DataTable during an enumeration</title><link>http://forum.strataframe.net/FindPost6323.aspx</link><description>This has nothing to do with data tables, but rather any type of collection in .NET.&amp;nbsp; If you had tried removing items from a collection inside of a loop you would have had the same issue.</description><pubDate>Fri, 26 Jan 2007 17:57:36 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: How to delete items from a DataTable during an enumeration</title><link>http://forum.strataframe.net/FindPost6319.aspx</link><description>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:&lt;br&gt;
&lt;br&gt;
[codesnippet]&lt;br&gt;
Dim myTable As DataTable&lt;br&gt;
'...fill table, two columns a string and an integer&lt;br&gt;
For Each myRow As DataRow In myTable.Rows&lt;br&gt;
  If myRow.Item(1) = 34 Then&lt;br&gt;
    myRow.Delete()&lt;br&gt;
  End If&lt;br&gt;
Next&lt;br&gt;
myTable.AcceptChanges&lt;br&gt;
[/codesnippet]&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
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.</description><pubDate>Fri, 26 Jan 2007 15:39:30 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: How to delete items from a DataTable during an enumeration</title><link>http://forum.strataframe.net/FindPost6318.aspx</link><description>It is because you are changing the contents of the value being enumerated.&amp;nbsp; You can't do this.&amp;nbsp; Just create an array or list that has all of the rows that you want to delete inside of the loop.&amp;nbsp; Then create another loop that cycles through the array you created to actually delete the rows.</description><pubDate>Fri, 26 Jan 2007 15:17:09 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>