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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14089342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:37:50  来源:igfitidea点击:

Refresh ComboBox Items, easiest way

c#winformsc#-4.0combobox

提问by Cool Brain

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 methodthat takes a List<string>and removes previous Items, then set this List<string>.

我用谷歌搜索了很多。也发现了很多。不幸的是,没有什么是直接的、简单的,最重要的是,简单。我希望有人写一个method需要 aList<string>并删除前一个Items,然后设置 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;
}

采纳答案by phnkha

You don't need albumList.Items.Clear();

你不需要albumList.Items.Clear();

This code works just fine

这段代码工作得很好

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

回答by Thorsten Dittmar

When bound to a data source,Items.Clear();doesn't work. This should be:

当绑定到数据源时,Items.Clear();不起作用。这应该是:

albumList.DataSource = list;

or to be sure:

或者确定:

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

回答by Fdej

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. You can then sort that IList reference and it will be reflected again within the combobox.

如果您想要一个可排序的组合框,您可以使用从 IList 继承的容器构造 BindingList,例如具有排序功能的 List。然后,您可以对该 IList 引用进行排序,它将再次反映在组合框中。

回答by DocWho

If you are using DataSource you need to clear items for the new list to appear. Since you can't clear while using DataSource you need to set it to null first.

如果您正在使用 DataSource,您需要清除项目以显示新列表。由于在使用 DataSource 时无法清除,因此需要先将其设置为 null。

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

回答by Ashvin Tam

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. 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.

将数据源设置为 null 并将列表重新分配给它的初始解决方案根本不起作用。尽管它确实清除了项目并在列表中显示了新项目,但它使得无法选择该列表中的任何项目。但是在互联网上挖掘后,我发现了一个旧的 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. See code below:

    if (albumList.DataSource == null) 
    { 
        albumList.DataSource = listExample; 
    } 
    else 
    {
        albumList.Refresh();
    }
    
  1. 在重新填充之前清除列表对象。见下面的代码:

    //adding the instantiation of the list object so you know what I'm clearing
    List<string> listExample = new List<string>();
    listExample.Clear();
    
  2. 刷新组合框,以便它选择新列表。见下面的代码:

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