Beytan Kurt
Beytan Kurt

Reputation: 2263

ComboBox Items Empty but DataSource Full

After binding a list to combobox, its dataSource.Count is 5 but, combobox item count is 0. how can it be?

I'm used to Web programming and this is in Windows Forms. So no combo.DataBind(); method exists.

The problem here is, I'm trying to set the selected item programmatically. Since I don't see the combo.Items collection filled, I cannot set the desired item.


Update

A total update is needed I guess:

So the problem is here; since after databound no items are there in the ItemCollection of combobox; I cannot search for one to match and set the appropriate one.

Here is a image for better understanding (But I'm pretty sure I'm missing sth simple)

screenshot

Upvotes: 16

Views: 13410

Answers (3)

Michael
Michael

Reputation: 1421

I had the same problem, but in my case it was caused by calling

combobox.Sorted = True

in InitializeComponent. I guess that call initializes Items, which then prevents the assignment to DataSource from updating it (Items).

Upvotes: 1

Paweł
Paweł

Reputation: 11

If you'd expand DataSource items in debuger, you'd probably notice that 1st element on list is null. That is why DataSource does not render ComboBox Items. Removing null items from the list should do all the work;

Upvotes: 1

Beytan Kurt
Beytan Kurt

Reputation: 2263

After adding ddl.BindingContext = new BindingContext(); before the BindingSource assignment, everything worked fine.

Upvotes: 26

Related Questions