Tom
You are having the same problem, I would recommend you code the loop the
same way as I demonstrated.
Remember if you delete row 1, row 2 is now row 1, as row 1 was removed from
the collection. However you incremented "i" so you are looking at what was
row 3 (the new row 2).
The other method to use is to start at the end of the collection and work
forward. (decrement "i" instead of incrementing it).
Hope this helps
Jay
Tom Gross said:
Hi Jay,
I'm having much the same problem, but I'm not sure if either of your
solutions would work for me in my case.
I have a for loop iterating through a dataview:
for (int i = 0; i < EventCount; i++)
{
row = EventsDataView;
EventID = row["EventID"];
if (eventisGood(EventID))
markEventForDeletion(EventID);
}
void markEventForDeletion(int EventID)
{
DataRow row = TagEventsDataSet.Tables["TagEvents"].Rows.Find(EventID);
row.Delete();
}
what I find is that my program crashes half-way through the dataview
(if there are 70 rows in the dataview it will crash at 35) so I can
see that I am actually deleting data underneath myself.
Incidentally, this isn't at all how I would expect ADO.NET to work,
based on what I have read (for example, page 241 of David Sceppa's
book Microsoft ADO.NET). The way I read it, *removing* a datarow
would actually remove it from the table, but *deleting* just flags it
as a pending deletion (if things worked the way I would expect, the
rows would not be deleted until the AcceptChanges method was called,
so now I'm wondering what AcceptChanges is for).
Again, I don't want to delete every row in my datatable, only some of
them. How am I supposed to iterate through the rows?