1

I want that the ListBox automatically show changes when I remove or add an object to the list which I use as its DataSource.

How can I connect a List<T> to a ListBox and see changes of underlying list in ListBox immediately?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Daniel Jo
  • 340
  • 2
  • 14
  • Possible duplicate of [how to bind a list to a combobox? (Winforms)](http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms) – Sayse Nov 10 '15 at 07:48
  • @Sayse I think the question is not a duplicate of that link :) The focus of that link is on setting a `List` as `DataSource` but the OP needs to see changes of list in its `ListBox` that means he should use an implementation of `IBindingList` or `BindingList`. – Reza Aghaei Nov 10 '15 at 08:11
  • @RezaAghaei - The accepted answre in that question mentions that but yes it doesn't explain it. Either way it wouldn't take long for the op to find a duplicate... "winforms databind list" had that duplicate as its first result – Sayse Nov 10 '15 at 08:13
  • @Sayse Thank you for your feedback, I think my answer is more straightforward answer for users. Hope you find it useful too :) – Reza Aghaei Nov 10 '15 at 08:18
  • @RezaAghaei - [MSDN: Winforms Databinding](https://msdn.microsoft.com/en-us/library/ef2xyb33%28v=vs.110%29.aspx) and [a second duplicate](http://stackoverflow.com/q/17615069/1324033) – Sayse Nov 10 '15 at 08:29
  • @Daniel Jo Just in case that you have forgot to accept the answer, it would be great if you can verify and accept the answer. It makes the post more useful for future readers. But if you believe it doesn't answer your question feel free to keep it as is or suggest improvements :) – Reza Aghaei Jan 28 '21 at 16:56

2 Answers2

5

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;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

See the following example where a List of strings is passed into a listBox instance.

public partial class Form1 : Form
{
List<string> _items = new List<string>(); // <-- Add this

public Form1()
{
    InitializeComponent();

    _items.Add("One"); // <-- Add these
    _items.Add("Two");
    _items.Add("Three");

    listBox1.DataSource = _items;
}
}
Rober
  • 726
  • 8
  • 27