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