问题
Combobox datasource has been assigned with
cmbCombobox.DataSource = myCollection
where myCollection has type MyCollection: List
How can I clear items in combobox?
回答1:
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.
cmbComboBox.DataSource = null;
If your combobox is not databound (no DataSource) then you can do
cmbComboBox.Items.Clear();
回答2:
http://support.microsoft.com/kb/327895
Me.ListBox1.DataSource = Nothing
This works for me. VB incorrectly advises the use of DBNull (which crashes).
回答3:
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:
If there is no DataSource, then you just clear the items:comboBoxAssignee.DataSource = null;
comboBoxAssignee.DataBindings.Clear();
comboBoxAssignee.Items.Clear();
回答4:
in asp.net you can do like this:
cbMyComboBox.Items.Clear();
Maybe it works and for winforms) Not sure
回答5:
I'm using Visual Studio 2012 and .net v4.5 and am creating winforms in VB. The following do not work:
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."
The following does work and I have used it in a number of upgrades to applications.
Me.cmboFromLoc.DataSource = Nothing
来源:https://stackoverflow.com/questions/14376577/clear-combobox-datasource-items