Best Way of Deleting All Rows in a DataView

G

Guest

If I create a DataView(i.e. dv) with a particular RowFilter and Sort, I
realize that I can't use a For Each to delete those rows because each
deletion changes the current view.

So that this won't work:
dim drv as DataRowView
for each drv in dv
drv.delete()
next



If however I collect an array of DataRowViews by using the FindRows of the
DataView and then using a snippet such as this(which deletes the rows in
reverse order) the deletion of the group of filtered rows works fine:

Dim dv As New DataView(TheTable, TheRowFilter, TheSort,
DataViewRowState.CurrentRows)
Dim drv() As DataRowView = dv.FindRows("SortKey")
For x = drv.GetUpperBound(0) To 0 Step -1
drv(x).Delete()
Next



However, this seems more complicated that it should be.

Therefore, what is the best way to delete all the rows in a particular
DataView?
 
W

W.G. Ryan eMVP

You probably want to hit the table that it's bound to directly and delete
those rows or if you must iterate the view, just do it with an index based
loop and delete the row one by one. That should do it for you.
 
G

Guest

The nice potential of using the DataView to do the deletions is the ability
to make a selection of rows via the RowFilter. You can't do that in the
DataTable. It would be nice if the DataView had a DeleteRows() method, that
way you could filter on the rows you wanted gone and then vaporize them in
one fell swoop.
 
K

Kevin Yu [MSFT]

Hi Michael,

I agree with Bill that it's better to delete the row from the original
DataTable. Because as you can see, if you delete the DataRowView in the
DataView, the DataView will change because of this deletion. Currently,
there is no DeleteRows method in the DataView. Sorry for the inconvenience.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
C

Cor Ligthert [MVP]

michael,

I am not forever sure if this in 1.x and 2.x the same, not everything is
working as dynamicly in 1.x as it should be.

However let us assume it is like that. Than the collection is everytime
created new. Therefore you have to do it bottom up.

For i as integer = dv.count -1 to 0 step -1
dv(i).delete
Next

I hope this helps,

Cor
 

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