Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox items.count doesn't match DataSource

I have a ComboBox that is bound to a DataSource. I want to dynamically add items to the ComboBox based on certain conditions. So what I've done is add the options to a new list, and then change the DataSource of the ComboBox like so:

cbo.DataSource = null;
cbo.DataSource = cbos;
cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";

Then, I check cbo.Items.Count, and it has not incremented - it does not equal the count of the DataSource. Any ideas what I can do here?

Note this is WinForms and not ASP.NET.

like image 522
Madeleine Avatar asked May 20 '09 13:05

Madeleine


People also ask

How many items can be added to a ComboBox?

The ComboBox can easily contain thousands of items. The 100 limit you are referring to is the visible portion which is displayed when the ComboBox drop down appears. Note that the performance is tied to what type of data is being populated within the ComboBox ; a complex object versus a simple string value.

How do I find the index of a ComboBox?

The Combobox widget allows you to create a dropdown list in which the list of items can be selected instantly. However, if you want to get the index of selected items in the combobox widget, then you can use the get() method. The get() method returns an integer of the selected item known as the index of the item.


1 Answers

If anyone experiences this problem on a dynamically added combobox, the answer is to ensure that you add the combobox to the controls of a container in the form.

By adding "this.Controls.Add(cbo);" to the code before setting the datasource, the problem goes away.

like image 51
Madeleine Avatar answered Sep 30 '22 08:09

Madeleine