PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Best Way of Deleting All Rows in a DataView

Reply

Best Way of Deleting All Rows in a DataView

 
Thread Tools Rate Thread
Old 12-02-2006, 03:24 PM   #1
=?Utf-8?B?bWljaGFlbA==?=
Guest
 
Posts: n/a
Default Best Way of Deleting All Rows in a DataView


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
  Reply With Quote
Old 12-02-2006, 09:23 PM   #2
W.G. Ryan eMVP
Guest
 
Posts: n/a
Default Re: Best Way of Deleting All Rows in a DataView

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
news46F85C7-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



  Reply With Quote
Old 13-02-2006, 01:10 AM   #3
=?Utf-8?B?bWljaGFlbA==?=
Guest
 
Posts: n/a
Default Re: Best Way of Deleting All Rows in a DataView

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
> news46F85C7-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

>
>
>

  Reply With Quote
Old 13-02-2006, 02:27 AM   #4
Kevin Yu [MSFT]
Guest
 
Posts: n/a
Default Re: Best Way of Deleting All Rows in a DataView

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."

  Reply With Quote
Old 13-02-2006, 08:53 AM   #5
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Best Way of Deleting All Rows in a DataView

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


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off