0

During the iteration process, I need to delete some rows.

foreach (DataRow dtRow in dtTable.Rows)
{

\\some code

    for(int i = dt.Rows.Count-1; i >= 0; i--)
       {
        DataRow dr = dt.Rows[i];
        if (dr["Column1"].ToString().Contains("some text"))
            dr.Delete();
       }
       dt.AcceptChanges();

\\some code 2

}

When the code reaches "some code 2" block, I get the error: "For Each Row: The collection has been changed, perhaps the enumeration operation will not be performed."

Can I update a table instance without exiting the current for each loop and starting a new one?

Thank you.

Foertsch
  • 105
  • 6
  • Does https://stackoverflow.com/questions/1591771/datatable-how-to-conditionally-delete-rows answer your question? – Alexander van Oostenrijk Aug 19 '21 at 09:41
  • @AlexandervanOostenrijk Hello. He doesn't get a similar error with collections as I have. I am interested in the possibility of modifying the iterated table so that it is taken into account in the upper for each loop. If you try to do what is written in my code , you will get an error that row no longer exists in the table. – Foertsch Aug 19 '21 at 09:47

0 Answers0