DataGridView DataError exception when removing items from bound list

Chris Hollman

Member
Joined
Nov 1, 2011
Messages
9
Location
Rome, New York, United States
Programming Experience
5-10
Hi,

I am using a DataGridView bound to a BindingSource which is itself bound to a List of objects. This all works well until I remove objects from the list. If I am scrolled down to the bottom of the DataGridView and then remove a bunch of items from the list it is bound to it generates a DataError exception because the length changed and the rows I am looking at no longer exist... I changed my code to handle that error, but now instead of popping up a message it SLOWLY erases the data in each of the newly invalid rows at the end of the table (I think each time it does one it generates a new DataError, which is why it is so slow).

How do I prevent this? I want to be able to dynamically (and programmatically) add and remove items from the table without the possibility of this error... I assumed this would be handled automatically as part of the data binding process but I guess not...

TYIA.
 
I found a potential solution to this, not sure if it is the right thing to do though... after I change the data in the list (specifically the number of elements) I can call the ResetBindings() method of the BindingSource class before refreshing the DataGridView...

Seems like this shouldn't be necessary and might be adding significant slow down though, I'd still like to know if there is a better answer.
 
Back
Top