4

There is following code:

public partial class Form1 : Form
{
    private List<string> names = new List<string> { "aa", "bb", "cc" };

    public Form1()
    {
        InitializeComponent();

        comboBox1.DataSource = names;
        comboBox1.DisplayMember = "Name";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        names.Add("dd");
    }
}

When i click on the button i want to add new value to source list and expect that combobox would be instantly updated to include that value. But it's not. Why? In general, what's is the easiest proper way to connect List to a combobox the way that adding/removing items in list affects combobox directly?

clumpter
  • 1,898
  • 6
  • 27
  • 38

2 Answers2

3

You can also use a BindingList which raises a ListChanged event when the content of the list is changed.

BindingList<string> names = new BindingList<string> { "aa", "bb", "cc" }; 

For the difference between ObservableCollection and BindingList see this answer, which is much better than what I can provide.


If you have to keep the data in a normal List you can wrap the list in a BindingSource like this:

private BindingSource source; //property
...
   source = new BindingSource(names, "");
   comboBox1.DataSource = source;

And then add to the source:

source.Add("3");

The BindingSource will add items to the underlying list and raise the ListChanged event.

If you cannot use that solution either, I cannot think of anything else than just resetting the DataSource when you update the list:

names.Add("dd");
comboBox1.DataSource = null;
comboBox1.DataSource = names;
Community
  • 1
  • 1
MAV
  • 7,260
  • 4
  • 30
  • 47
  • @Javidan Yes. But it doesn't raise an event if it is the custom types members you are changing. (eg. if you are displaying a property called `name` from an object and you change the value of `name`). – MAV Apr 23 '13 at 19:02
  • I read the link you given in your solution, really if not your answer i will not be aware about it. Thanks – Javidan Apr 23 '13 at 19:42
  • This works. But i need to leave List since that is just an example and in real project i can't change List to BindingList. Is there any way? – clumpter Apr 23 '13 at 19:45
  • You can use `BindingList` and create property like `MyList` and there put only `get` part returns like `BindingList.ToList()`. – Javidan Apr 23 '13 at 20:01
  • @clumpter I have added a possible solution that does not require any changes to the list. But I will recommend looking into another approach. – MAV Apr 23 '13 at 20:06
  • @MAV solution with resetting datasource works good but it doesn't look right if you know what i mean. – clumpter Apr 23 '13 at 20:11
  • @clumpter I know what you mean. Found a third solution if you can use that one. – MAV Apr 23 '13 at 20:23
  • @MAV for now i ended up with resetting datasource. – clumpter Apr 25 '13 at 17:41
1

It occurs because when your List is changed there is no event occurred to inform ComboBox about changes. Use ObservableCollection<string> instead of List<string> . It is like List, but have features. ObservableCollection<string> follows INotifyCollectionChanged, INotifyPropertyChanged interfaces, and these are needed events.

Javidan
  • 566
  • 1
  • 9
  • 25