Databinding to List – See changes of data source in ListBox, ComboBox

In Windows Forms, in a scenario that you want to see changes of data source in the bound list control, like ComboBox, ListBox or DataGridView (complex two-way data binding), you should use a class that implements IBindingList interface as DataSource of data binding. The most suitable implementation is BindingList<T>. This way each add/remove in the underlying data source of your control will be visible in the control immediately.

Please keep in mind, using BindingList<T> allows the bound control see added or removed items, but to see also property changes immediately, T should implement INotifyPropertyChanged. This way your your controls will be notified of PropertyChanged and always show fresh data.

Note 1 – Does ObservableCollection solve two-way data binding Probelm?

In Windows Form, a common mistake is using ObservableCollection that will not work for this requirement since it doesn’t implement IBindingList.

Note 2 – Does BindingSource solve two-way data binding problem?

If the underlying data source of BindingSource doesn’t implement IBindingList<T> it doesn’t solve two way data binding problem. You need to notify controls to reload data from binding source, so you can call ResetBindings method of BindingSource. This way, bound controls will reload data from data source and show the latest data:

this.bindingSource1.ResetBindings(false);

Note 3 – I should use List<T>. How can I solve the problem using List<T>?

If you must use List<T>, then you can reset the databinding of your list box when you need, for example after each change you should assign its DataSource to null and again to the list of data:

this.listBox1.DataSource = null;
this.listBox1.DataSource = list;

Leave a Comment