Binding to a property of an item from another bound list

Go To


I've got a list of Validation objects - validations.

public class Validation
       public IList<KeyValuePair<string, string>> Arguments
       { //(...) }

On a form there is a list bound to validations list and a DataGridView bound to Arguments list of current Validation from validations list. I allow user to edit selected Validation object in a dialog window. The user can modify Arguments collection. After closing the window the items displayed in the DataGridView should refresh. They don't. Also if the Arguments list is empty after editing, the IndexOutOfRangeException is thrown.

How can I make it work?

2009-06-16 10:10
by agnieszka


There are several important interfaces for data-binding; in particular, IBindingList, which has the ListChanged event that DataGridView can listen for.

Is it possible to change the concrete list to a BindingList<T>? That should give you most of this for free? You don't need to change the return type, as BindingList<T> : IList<T>, and DataGridView only knows about the actual object (it doesn't care that you call it IList<T>).

The other pragmatic option is simply to reset the data-binding on the DataGridView - perhaps set the DataSource to null and then back:

object tmp = grid.DataSource;
grid.DataSource = null;
grid.DataSource = tmp; // low-tech data-source reset
2009-06-16 10:41
by Marc Gravell
hehehe you're my hero of the day ;) thank - agnieszka 2009-06-16 10:45
do you know why it doesn't work properly with IList - agnieszka 2009-06-16 10:46
IList isn't the issue - that is just an interface; it is the concrete type that matters. If the concrete type is List or ArrayList, then neither implements IBindingList, so neither can proactively tell the DataGridView that changes have happened - Marc Gravell 2009-06-16 10:48
sorry, my mistake, I meant why doesn't it work with a Lis - agnieszka 2009-06-16 10:50