DataGrid ArgumentOutofRange Exception

  • Thread starter Thread starter Sean Carpenter
  • Start date Start date
S

Sean Carpenter

I have a DataGrid in my application that is bound to a custom collection of
business objects. The collection inherits from CollectionBase so is
bindable. My problem occurs when I remove an item from the collection.
After the removal, any time I click on the grid, I get an un-catchable
ArgumentOutofRange exception. I tried a few workarounds:

1. Before removing an item from the collection, I set the DataSource of the
grid = Nothing. Then I removed the item, set the DataSource back to the
collection, and called Refresh. This caused the grid to re-display with the
removed item gone, but as soon as I clicked on the grid, I got the
exception.

2. I added an IBindingList implementation to my collection and set
SupportsChangeNotification to True. I raised the ListChanged event in the
Add and RemoveAt methods of the collection. This caused the grid to update
correctly without resetting the DataSource, but I still got the exception
when clicking on the grid.

By the way, I get the exception whether or not there are any event handlers
attached to grid. Any advice would be appreciated.

Thanks,
Sean Carpenter
 
When exception is thrown (make sure you do not catch it in code), check the
Call Stack window. Right-click on it and ensure that "Show non-user code" is
on. What are the top few lines of the stack?
 
Thanks, Alex, the call stack is below. I took a quick look in Reflector and
it seems like the listposition in the underlying CurrencyManager is still
set to 1 (this is the row I had clicked on before the removal). This is
after I set the DataSource to Nothing and back to the collection.

mscorlib.dll!System.Collections.CollectionBase.System.Collections.IList.get_Item(int
index = 1) + 0x18 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.EndCurrentEdit()
+ 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager._ChangeRecordState(int
nPositionNew = 0, bool fValidating = true, bool fEndCurrentEdit = true, bool
fFirePositionChange = true, bool fPullData = true) + 0x4b bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.set_Position(int
value = 0) + 0x3b bytes
System.Windows.Forms.DataGrid.dll!System.Windows.Forms.DataGrid.OnCurrentCellChanged(System.EventArgs
e = {System.EventArgs}) + 0x16 bytes
System.Windows.Forms.DataGrid.dll!System.Windows.Forms.DataGrid._OnCurrentCellChanged()
+ 0xb bytes
System.Windows.Forms.DataGrid.dll!System.Windows.Forms.DataGridCurrentCell._MoveTo(int
irow = 0, int icol = 1, bool fDrawAndUnselect = true) + 0x1c4 bytes
System.Windows.Forms.DataGrid.dll!System.Windows.Forms.DataGridCurrentCell._MoveToChecked(int
irow = 0, int icol = 1) + 0x6d bytes
System.Windows.Forms.DataGrid.dll!System.Windows.Forms.DataGrid.OnMouseDown(System.Windows.Forms.MouseEventArgs
mea = {System.Windows.Forms.MouseEventArgs}) + 0x84 bytes

Sean Carpenter
 
In my test it worked as expected. Can I see somehow your IBindingList and
IList implementation? POst here or email.
 
Back
Top