foreach behavior changed in Framework 2.0

G

Guest

I have the following code in a C# project:

DataView dv = new DataView( dsModels.Tables[_INDVARS_TABLE_NAME], "", "",
DataViewRowState.CurrentRows);
foreach ( DataRowView drv in dv)
drv.Row.Delete();

This code worked fine in VS 2003 (Framework 1.1). However in VS 2005
(Framework 2.0) it behaves differently. It looks to me that the call to
drv.Row.Delete() actually deletes the next item in the list. I tested this by
creating two foreach loops, the first just outputs an ordinal value that gets
incremented each time through the loop and a unique id of the row; the second
loop outputs the same information and subsequently deletes the row with the
drv.Row.Delete() statement. I put a try/catch block around the output
statement in the second loop and in the catch block I output the ordinal
value and the error.

number of items in the dataview: 13
0: id = 861
1: id = 961
2: id = 966
3: id = 1089
4: id = 3194
5: id = 40
6: id = 3190
7: id = 3193
8: id = 3195
9: id = 422
10: id = 3192
11: id = 2477
12: id = 768
0: id = 861
1: id = 966
2: id = 3194
3: id = 3190
4: id = 3195
5: id = 3192
6: id = 768
7: error = There is no row at position 7.
8: error = There is no row at position 8.
9: error = There is no row at position 9.
10: error = There is no row at position 10.
11: error = There is no row at position 11.
12: error = There is no row at position 12.

It appears that the first call to drv.Row.Delete() actually deletes the row
with id 961 (the "next" row) rather than the row with id 861 (the "current"
row). To make things worse, it tries to keep iterating past the end which is
where the error messages are coming from.

I could not find any documentation regarding this behavior but I wasn't sure
exactly where to look. Any insight would be appreciated.

Thanks
 
G

Guest

This is a known issue, I don't believe there is a KB article on it.

The workaround is something like
while(0 < dv.Count) { dv[dv.Count-1].Row.Delete(); }

notes:
1. depending on the filter and expressions, deleting a row can actually
add/remove rows from a DataView.
2. while(0 < dv.Count) { dv[0].Row.Delete(); } has performance issues in V2.0
 
K

Kevin Yu [MSFT]

Hi Rich,

Yes, this is a known issue in .NET 2.0. It is by design and seems to be a
break change in version 2.0.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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