Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh ComboBox Items, easiest way

I've googled a lot. Found a lot as well. Unfortunately nothing is straight, easy and most importantly, simple. I want some guy write a method that takes a List<string> and removes previous Items, then set this List<string>.

Currently I have a method but it's not error free.

public void refreshList(List<string> list){
    albumList.Items.Clear();
    albumList.DataSource =  list;
}
like image 286
Cool Brain Avatar asked Dec 30 '12 08:12

Cool Brain


1 Answers

For anyone still wondering.

You can use a BindlingList and BindingSource.

BindingList<YOUR_CLASS_TYPE> bindinglist = new BindingList<YOUR_CLASS_TYPE>()
BindingSource bSource = new BindingSource();
bSource.DataSource = bindinglist;
ComboBox.DataSource = bSource;

You add all items to your bindinglist and they will be automatically updated within your combobox.

If you want a sortable combobox you can construct the BindingList with a container that inherits from IList, like List that has a sort function. You can then sort that IList reference and it will be reflected again within the combobox.

like image 197
Fdej Avatar answered Sep 30 '22 19:09

Fdej