简体   繁体   中英

Clear combobox datasource items

Combobox datasource has been assigned with

cmbCombobox.DataSource = myCollection

where myCollection has type MyCollection: List

How can I clear items in combobox?

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();

http://support.microsoft.com/kb/327895

Me.ListBox1.DataSource = Nothing

This works for me. VB incorrectly advises the use of DBNull (which crashes).

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();

If there is no DataSource, then you just clear the items:

comboBoxAssignee.Items.Clear();

in asp.net you can do like this:

cbMyComboBox.Items.Clear();

Maybe it works and for winforms) Not sure

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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