0

While I am trying to bind data to my combobox from an object on the designer,the Items are not displayed.enter image description here

But they are displayed when I specify from the code behind...enter image description here

How I can bind my collection from the designer??

Mohd Anzal
  • 157
  • 8
  • Probably you just forgot to set `itemsBindingSource.DataSource = Items;`. Add this line of code after loading `Items`. – Reza Aghaei Oct 08 '16 at 09:59
  • In Windows Forms, in a scenario that you want view changes of data source in the bound list control (complex two-way data binding), you should use a class that implements `IBindingList` as `DataSource` and the the most suitable implementation is `System.ComponentModel.BindingList`. A common mistake is using `ObservableCollection` that will not work for this requirement since it doesn't implement `IBindingList`. You may find this post useful: [Connect List to a ListBox](https://stackoverflow.com/questions/33623991/connect-listt-to-a-listbox) – Reza Aghaei Oct 08 '16 at 10:20

2 Answers2

1

If you want to bind items from designer Select -> Item Collection FROM Property Window pic

Add Programatically from list

 List<string> values = new List<string>();

    private void AddItemProg()
    {
        values.Add("Name");
        values.Add("Age");
        values.Add("DOB");
        values.Add("Address");

        comboBox1.Items.Clear();

        for (int nIndex = 0; nIndex < values.Count; nIndex++)
        {
            string v = values[nIndex];
            comboBox1.Items.Add(v);
        }
    }
SH7
  • 732
  • 7
  • 20
1

You doesn't need use observablecollection to bind UI in windows form application. Just set the combobox.ItemSource = List<string> . When you want to get current value just use combobox.SelectedItem or combobox.SelectedValue get current value.
(ps. observable property should have get and set method, in the set method you need call a method RaisePropertyChanged("propertyname"), for that you also need do some change in UI part and import someting.(something like that, i don't remember exactly how it works, but it's complex.

Bucketcode
  • 461
  • 2
  • 13
  • There is no difference between a `List` and an `ObservableCollection` in data-binding with windows forms. – Reza Aghaei Oct 08 '16 at 10:29
  • BindingList and ObservableCollection both works and the items are displayed in combobox..I just want to know that whether I can reference it from the Designer/Properties..but still failed..I understand now,we cant do that!!.. – Mohd Anzal Oct 08 '16 at 17:16