views:

39

answers:

1

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?

A: 

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
Marc Gravell
hehehe you're my hero of the day ;) thanks
agnieszka
do you know why it doesn't work properly with IList?
agnieszka
IList isn't the issue - that is just an interface; it is the concrete type that matters. If the concrete type is List<T> or ArrayList, then neither implements IBindingList, so neither can proactively tell the DataGridView that changes have happened.
Marc Gravell
sorry, my mistake, I meant why doesn't it work with a List
agnieszka