views:

32

answers:

1

I have a List of an object, set to DataSource of a BindingSource, set to DataSource of a DataGridView. Then I added a DataGridViewButtonColumn which I want a click to remove that record from the collection. So I simply call collection.RemoveAt(e.rowIndex) from the DataGridView CellClick event. However this produces an IndexOutOfRange Exception; "1 does not have a value"..

So is happening here? I have maybe a vague idea, but would like to understand exactly which events are failing etc.. and what do i need to do to perform this action properly?

EDIT: This seems not to happen if I use a BindingList.. But when using a List, I get this problem..

+1  A: 

The reason you're seeing the error is because you're ultimately binding to a List. List doesn't include the notifications of changes that your DataGridView needs to receive to reflect the changes you make.

If you really want to work around this you could do the following: just before you make a changes to your List, set the BindingSource's DataSource property to Nothing. When you're done making changes reset your List to the DataSource.

This is a pretty ugly solution as you can imagine. What you'll want to do this bind to an object that implements IBindingList, either a custom collection you create or a BindingList.

Here's a reasonable starting point for investigating this further:
DataGridView Control Overview (Windows Forms)

Jay Riggs
I see.. Thanks! :)
bretddog