.net 清除组合框数据源项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14376577/
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
Clear combobox datasource items
提问by hellboy
Combobox datasource has been assigned with
组合框数据源已分配
cmbCombobox.DataSource = myCollection
where myCollection has type MyCollection: List
其中 myCollection 的类型为 MyCollection: List
How can I clear items in combobox?
如何清除组合框中的项目?
回答by Alan
When you data bind a control, it will synchronize with that collection. In order to clear the items in your ComboBox set it's DataSource to null.
当您数据绑定一个控件时,它将与该集合同步。为了清除 ComboBox 中的项目,请将它的 DataSource 设置为 null。
cmbComboBox.DataSource = null;
If your combobox is notdatabound (no DataSource) then you can do
如果您的组合框未绑定数据(无数据源),那么您可以执行
cmbComboBox.Items.Clear();
回答by Andrew
http://support.microsoft.com/kb/327895
http://support.microsoft.com/kb/327895
Me.ListBox1.DataSource = Nothing
This works for me. VB incorrectly advises the use of DBNull (which crashes).
这对我有用。VB 错误地建议使用 DBNull(它会崩溃)。
回答by Ian
Here is how it worked for me:
这是它对我的工作方式:
If your combobox has a DataSource, then simply assigning to a null data source should be enough. However, many times you have to clear the bindings manually:
如果您的组合框有一个数据源,那么简单地分配给一个空数据源就足够了。但是,很多时候您必须手动清除绑定:
comboBoxAssignee.DataSource = null;
comboBoxAssignee.DataBindings.Clear();
如果没有数据源,那么您只需清除项目:comboBoxAssignee.DataSource = null;
comboBoxAssignee.DataBindings.Clear();
comboBoxAssignee.Items.Clear();
comboBoxAssignee.Items.Clear();
回答by EasyE
I'm using Visual Studio 2012 and .net v4.5 and am creating winforms in VB. The following do not work:
我正在使用 Visual Studio 2012 和 .net v4.5 并在 VB 中创建 winforms。以下不起作用:
me.combobox.Datasource = null
me.combobox.Items.clear()
Using Null isn't even an option for the datasource and I get the following error when I tried items.clear(); "Items collection cannot be modified when the DataSource property is set."
使用 Null 甚至不是数据源的选项,当我尝试 items.clear(); 时出现以下错误;“设置 DataSource 属性后,无法修改项目集合。”
The following does work and I have used it in a number of upgrades to applications.
以下确实有效,我已经在许多应用程序升级中使用了它。
Me.cmboFromLoc.DataSource = Nothing
回答by makambi
in asp.net you can do like this:
在 asp.net 中,您可以这样做:
cbMyComboBox.Items.Clear();
Maybe it works and for winforms) Not sure
也许它适用于winforms)不确定

