Clear combobox datasource items

怎甘沉沦 提交于 2019-12-04 07:30:04

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!