Delete DataView rows from DataTable (should be simple)

  • Thread starter Thread starter MattB
  • Start date Start date
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
 
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.
 
Back
Top