简体   繁体   中英

Binding to a property of an item from another bound list

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM