Deleting from a DataView

M

Mike Mattix

I have an issue of attempting to Delete the DataRowViews from a
DataView. I have created a RowFilter to restrict the DataRowViews to
the group that I want to delete. When I delete the DataRowViews in a
for-next loop I get an error that a row does not exist in Pos n where n
seems to be half of the total number of DataRowViews in the DataView.

Example

dim dv as DataView
dim drv as DataRowView
dim dt as DataTable

dv = New DataView(dt)

dv.RowFilter = "Some Filter"

for each drv in dv
drv.Delete
next


It seems as though the enumeration of DataRowViews breaks down when you
delete a member from the DataView. ie:

row 0 - Pos 0
row 1 - Pos 1
row 2 - Pos 2
row 3 - Pos 3

Delete first row (row 0 - enumerator:0)

row 1 - Pos 0
row 2 - Pos 1
row 3 - Pos 2

Delete next row (row 2 - enumerator:1)

row 1 - Pos 0
row 3 - Pos 1

Delete next row (throws exception - enumerator:2)


I guess I could get the count and create a for loop to negatively
iterate through the items but isn't there an better(easier) way???

Thanks for ANY assistance.

Mike Mattix
 
M

Mike Mattix

Trevor,

I could but I am trying to abstract the SQL away from my application.
This is kinda the way ADO.NET wants you to work anyway. The problem is
that the enumerator in the for each - next does not keep in sync with
the index of the view items array. It seems like a bug and I was
wondering if anyone else had seen this or if they had a work around for
working with disconnected datatables/datasets..

thanks

Mike
 
M

Marc Scheuner [MVP ADSI]

I have an issue of attempting to Delete the DataRowViews from a
DataView. I have created a RowFilter to restrict the DataRowViews to
the group that I want to delete. When I delete the DataRowViews in a
for-next loop I get an error that a row does not exist in Pos n where n
seems to be half of the total number of DataRowViews in the DataView.

While don't you just delete the row at position no. 0, as long as
there still are rows in the DataView?

while (dv.Count > 0)
{
dv.Delete(0);
}

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
M

Mike Mattix

Marc,

Thanks, that was the type of solution I was looking for.
But it does seem to be an enumerator bug to me...

Mike
 
M

Marc Scheuner [MVP ADSI]

Thanks, that was the type of solution I was looking for.
But it does seem to be an enumerator bug to me...

Well, enumerators are fine, as long as you don't start deleting stuff
out from under their feet ;-) That's always a tad messy

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 

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