I don’t understand why this code does not work.
foreach (DataRow dataRow in dataTable.Rows)
{
if (true)
{
dataRow.Delete();
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Even though
DataRow.Deletedoesn’t modify the state of the collection, Microsoft documentation states that you shouldn’t call it while iterating over the collection:The best solution is usually to create a separate collection (e.g. a
List<DataRow>) of items you want to remove, and then remove them after you’ve finished iterating.This is also the solution for situations where you want to remove items from a collection, as most collections in .NET don’t allow you to change the contents of the collection while you’re iterating over it.