Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...