How can I delete specific DataRows within a loop of a DataTable rows which meet a custom condition -lets say the rows having an index of even number-? (Without using LINQ)
Thanks
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.
It depends on what you mean by ‘delete’.
If you mean mark them as deleted, just call the
Delete()method on each row as you visit it in your loop. You then need to callAcceptChanges()on the data table to finalize the delete – presumably after you update your database (if one is involved).If you mean remove it from the DataTable, then you need to do so in two passes:
It’s worth pointing out that you can always use the first method to remove rows – since marking rows as
Deletedand then accepting changes will automatically remove them from the table. But, sometimes it is more clear and efficient to simply remove theDataRowobjects from theRowscollection.