Ilya Tumanov [MS] wrote:
First of all, you've assumed number of rows in the DataTable is the same
as number of rows in the DataView/DataTable. You can compare these in a
loop to find our that's not the case even though you're not using
filtering and sorting. Indexes also can't possibly be the same since
number of rows is different.
Yeah, that would be a potential source of problems. It slipped in from an
old version of my code. Thanks for pointing it out.
Next wrong assumption is what row indexes in the DataGrid would not
change while this loop is executed.
That is also not the case as indexes would change as soon as first row is
deleted:
I=1, so far so good. About to delete selected row:
I DataGrid DataView DataTable
0 1 1 1
2 3 3 3 --------
Selected
3 4 4 4
After first delete you've got into total mess (I'm assuming you're
incrementing 'i' at the end of loop):
I=2, row just deleted row @1:
I DataGrid DataView DataTable
0 1 1 1
1 3 3 2 -------- Row is
in deleted state in DT, selected in DG/DV and they are different rows.
4 4
Note you would skip row '3' because it would move to index 1 you've
already processed. Also now you have 4 rows in the DataTable (one in
deleted state) and only 3 of them in the DataView/DataTable. Inevitably
you would increment row index to 3 which would generate an exception.
That's also very true. Rather unfortunately, I just failed to copy/paste
my loop in its eternity. I beg your pardon for this. I am pretty tired
already. The actual code looks like this:
for(int i = 0; i <
((DataTable)dataGridMain.DataSource).DefaultView.Count; )
if(dataGridMain.IsSelected(i))
{ ((DataTable)dataGridMain.DataSource).DefaultView.Delete(i);
}
else
i++;
So that was not the problem either.
As I mentioned in my previous posts the problem occurs if and only if I
select cells from certain columns of the DataGrid before attempting the
delete any rows. If I just select some rows and delete them the code above
runs smoothly.
Actually, I just narrowed down the source of the problem. I get the
exception only if I select a cell in the rightmost column of the DataGrid
before I try to delete some rows. Besides, it happens only if the
rightmost column is of certain size. I noticed that although no horizontal
scroll is displayed the rightmost column is slightly off the limits of the
DataGrid and when I perform a selection in it the grid gets automatically
scrolled slightly to the right.
Here is how it goes in code:
DataGridTextBoxColumn len = new DataGridTextBoxColumn();
len.HeaderText = "Length";
len.MappingName = "Length";
len.Width = (int)(0.1 * this.dataGridMain.Width);
ts_songs.GridColumnStyles.Add(len);
If I change that 0.1 to some other value the problem vanishes. It looks
like I got exactly the size that's problematic
So, overall, I think that the ArgumentOutOfRangeException actually has
nothing to do with the indexes of the DataView and the DataTable. It looks
more like something else in the DataGrid goes wrong. Is anybody aware of
such a bug? I am really unwilling to release like this without knowing
what exactly causes the problem.