PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Best Way of Deleting All Rows in a DataView
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Best Way of Deleting All Rows in a DataView
![]() |
Best Way of Deleting All Rows in a DataView |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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? -- Michael Hockstein |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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. "michael" <howlinghound@nospam.nospam> wrote in message news 46F85C7-78AC-4D64-AC78-20D77A3D98BF@microsoft.com...> 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? > -- > Michael Hockstein |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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. -- Michael Hockstein "W.G. Ryan eMVP" wrote: > 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. > "michael" <howlinghound@nospam.nospam> wrote in message > news 46F85C7-78AC-4D64-AC78-20D77A3D98BF@microsoft.com...> > 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? > > -- > > Michael Hockstein > > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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." |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

46F85C7-78AC-4D64-AC78-20D77A3D98BF@microsoft.com...
