Clear combobox datasource items

吃可爱长大的小学妹 提交于 2019-12-06 04:44:46

问题


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:

comboBoxAssignee.DataSource = null;

comboBoxAssignee.DataBindings.Clear();

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

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

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