简体   繁体   English

刷新ComboBox项目,最简单的方法

[英]Refresh ComboBox Items, easiest way

I've googled a lot. 我google了很多。 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> . 我想要一些人写一个method ,它接受一个List<string>并删除以前的Items ,然后设置这个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;
}

For anyone still wondering. 对于任何仍在疑惑的人。

You can use a BindlingList and BindingSource. 您可以使用BindlingList和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. 如果你想要一个可排序的组合框,你可以用一个继承自IList的容器构造BindingList,比如具有sort函数的List。 You can then sort that IList reference and it will be reflected again within the combobox. 然后,您可以对IList引用进行排序,它将在组合框中再次反映出来。

You don't need albumList.Items.Clear(); 你不需要albumList.Items.Clear();

This code works just fine 这段代码工作得很好

public void refreshList(List<string> list){
    albumList.DataSource =  list;
}

When bound to a data source, Items.Clear(); 绑定到数据源时, Items.Clear(); doesn't work. 不起作用。 This should be: 这应该是:

albumList.DataSource = list;

or to be sure: 或者肯定:

albumList.DataSource = null;
albumList.DataSource = list;

If you are using DataSource you need to clear items for the new list to appear. 如果您使用的是DataSource,则需要清除要显示的新列表的项目。 Since you can't clear while using DataSource you need to set it to null first. 由于在使用DataSource时无法清除,因此需要先将其设置为null。

albumList.DataSource = null;
albumList.Items.Clear();
albumList.DataSource = list;

I've been working on a task with a similar problem like this 我一直在处理类似这样的问题的任务

The initial solution to set the datasource to null and to reassign the list back to it didn't work at all. 将数据源设置为null并将列表重新分配给它的初始解决方案根本不起作用。 Though it did clear the items and show up the new items in the list, it made selection of any of the items in that list impossible. 虽然它确实清除了项目并在列表中显示了新项目,但它无法选择该列表中的任何项目。 But upon digging around the internet, I found an old VB solution that ended up working and having the intended effect. 但是在浏览互联网时,我发现了一个旧的VB解决方案,它最终起作用并具有预期的效果。

Here's what you need to do: 这是你需要做的:

  1. Clear the list object before repopulating it. 在重新填充列表对象之前清除它。 See code below: 见下面的代码:

     //adding the instantiation of the list object so you know what I'm clearing List<string> listExample = new List<string>(); listExample.Clear(); 
  2. Refresh the comboBox, so that it picks up the new list. 刷新comboBox,以便它获取新列表。 See code below: 见下面的代码:

     if (albumList.DataSource == null) { albumList.DataSource = listExample; } else { albumList.Refresh(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM