Delete datarowview objects from a dataview

J

Jason L James

Hi all,

I have a problem when attempting to
delete datarowview obecjts from a
data view

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
thisDRV.Delete()
Next

When I execute this code I get an
IndexOutOfRangeException exception. However,
if I print the first column of the datarowviews they all
exist.

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
debug.writeline(thisDRV(0))
Next

Is there something funny happeneing
with the deleting of a datarowview from a
dataview? Is this the correct way to delete
a subset of rows from a datatable?

Many thanks,

Jason.
 
M

Miha Markic [MVP C#]

Hi Jason,

You really shouldn't modify the enumerator in foreach (you are deleting rows
while traversing the array).
Instead, do a
for (int i=mydv.Count-1; i>=0; i--)
mydv.Delete();
 
J

Jason L James

Miha,

thanks for the advice. I check it out and let you
know how I get on.

Regards,

Jason.

Hi Jason,

You really shouldn't modify the enumerator in foreach (you are deleting rows
while traversing the array).
Instead, do a
for (int i=mydv.Count-1; i>=0; i--)
mydv.Delete();

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Jason L James said:
Hi all,

I have a problem when attempting to
delete datarowview obecjts from a
data view

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
thisDRV.Delete()
Next

When I execute this code I get an
IndexOutOfRangeException exception. However,
if I print the first column of the datarowviews they all
exist.

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
debug.writeline(thisDRV(0))
Next

Is there something funny happeneing
with the deleting of a datarowview from a
dataview? Is this the correct way to delete
a subset of rows from a datatable?

Many thanks,

Jason.
 
J

Jason L James

Miha,

things worked fine when I copied your code segment.

However, when I first coded your changes I reversed the
direction of the for loop to move from 0 to myDV.Count-1. This
failed with the same error. Can you explain why that should
be the case or refer me to an online resource that could.

Many thanks,

Jason.

Hi Jason,

You really shouldn't modify the enumerator in foreach (you are deleting rows
while traversing the array).
Instead, do a
for (int i=mydv.Count-1; i>=0; i--)
mydv.Delete();

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Jason L James said:
Hi all,

I have a problem when attempting to
delete datarowview obecjts from a
data view

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
thisDRV.Delete()
Next

When I execute this code I get an
IndexOutOfRangeException exception. However,
if I print the first column of the datarowviews they all
exist.

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
debug.writeline(thisDRV(0))
Next

Is there something funny happeneing
with the deleting of a datarowview from a
dataview? Is this the correct way to delete
a subset of rows from a datatable?

Many thanks,

Jason.
 
M

Miha Markic [MVP C#]

Hi Jason,

Take an example, there are three items, so you will practically iterator
from 0 to 2, right.
Now, you delete 1st record, so there are only 2 records remaining (out of
three) but you will stilly iterate from 0 to 2.
So, when you came to 3rd record - there is no more record, since you deleted
it.

if you want to go from 0 to 2 then you might do:
for (int i=0; i<myDV.Count-1; i++)
myDV[0].Delete(); // always deletes the 1st record

Since there is always 1st record, you won't get an exception.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Jason L James said:
Miha,

things worked fine when I copied your code segment.

However, when I first coded your changes I reversed the
direction of the for loop to move from 0 to myDV.Count-1. This
failed with the same error. Can you explain why that should
be the case or refer me to an online resource that could.

Many thanks,

Jason.

Hi Jason,

You really shouldn't modify the enumerator in foreach (you are deleting
rows
while traversing the array).
Instead, do a
for (int i=mydv.Count-1; i>=0; i--)
mydv.Delete();

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Jason L James said:
Hi all,

I have a problem when attempting to
delete datarowview obecjts from a
data view

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
thisDRV.Delete()
Next

When I execute this code I get an
IndexOutOfRangeException exception. However,
if I print the first column of the datarowviews they all
exist.

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
debug.writeline(thisDRV(0))
Next

Is there something funny happeneing
with the deleting of a datarowview from a
dataview? Is this the correct way to delete
a subset of rows from a datatable?

Many thanks,

Jason.

 
J

Jason L James

Miha,

many thanks for the explanation. I guess I assumed that the
ordinal position of the record wouldn't change but since it
is reducing the size of the myDV object after each deletion
it is.

Much appreciated.

Regards,

Jason.

Hi Jason,

Take an example, there are three items, so you will practically iterator
from 0 to 2, right.
Now, you delete 1st record, so there are only 2 records remaining (out of
three) but you will stilly iterate from 0 to 2.
So, when you came to 3rd record - there is no more record, since you deleted
it.

if you want to go from 0 to 2 then you might do:
for (int i=0; i<myDV.Count-1; i++)
myDV[0].Delete(); // always deletes the 1st record

Since there is always 1st record, you won't get an exception.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Jason L James said:
Miha,

things worked fine when I copied your code segment.

However, when I first coded your changes I reversed the
direction of the for loop to move from 0 to myDV.Count-1. This
failed with the same error. Can you explain why that should
be the case or refer me to an online resource that could.

Many thanks,

Jason.

Hi Jason,

You really shouldn't modify the enumerator in foreach (you are deleting
rows
while traversing the array).
Instead, do a
for (int i=mydv.Count-1; i>=0; i--)
mydv.Delete();

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi all,

I have a problem when attempting to
delete datarowview obecjts from a
data view

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
thisDRV.Delete()
Next

When I execute this code I get an
IndexOutOfRangeException exception. However,
if I print the first column of the datarowviews they all
exist.

Dim mydv As New DataView
Dim thisDRV As DataRowView

mydv.Table = myDS.Tables("CountedItem")
mydv.RowFilter = "ilID = '" & lID & "'"
For Each thisDRV In mydv
debug.writeline(thisDRV(0))
Next

Is there something funny happeneing
with the deleting of a datarowview from a
dataview? Is this the correct way to delete
a subset of rows from a datatable?

Many thanks,

Jason.


 

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