Delete DataView rows from DataTable (should be simple)

M

MattB

I'm using vb.net, but if you only have a c# answer, feel free to offer it!.

Anyway, I have a DataTable and a DataView which is a subset I have ID'd
to be deleted from that DataTable. I tried looping through the DataView
and deleting rows like this:

Dim dvAuto As New DataView
dvAuto.Table = dtCart
dvAuto.RowFilter = "AutoAdd = True"
If dvAuto.Count > 0 Then
Dim dr As DataRow
For Each dr In dvAuto.Table.Rows
dr.Delete()
Next
End If

But obviously it didn't work. I suspected modifying a collection while
looping through it would be a no-no and I was right (but tried anyway).

Without being able to issues a SQL delete like:
delete from dtCart where AutoAdd = True

I'm just not sure how to do this. How is this typically done? Thanks!

Matt
 
C

Chad Z. Hower aka Kudzu

MattB said:
Dim dr As DataRow
For Each dr In dvAuto.Table.Rows
dr.Delete()
Next
End If

But obviously it didn't work. I suspected modifying a collection while
looping through it would be a no-no and I was right (but tried
anyway).

Use a for loop and count backwards. If ir you want to clear all, just call .Clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top