ComboBox items.count doesn't match DataSource

Did you check the Count immediately or at a later time? There is the possibility that the ComboBox does not actually update it's contents until there is an operation such as a UI refresh and hence the count will be off until that time.

On case where this may happen is if you update the DataSource before the Handle is created for the ComboBox. I dug through the code a bit on reflector and it appears the items will not be updated in this case until the ComboBox is actually created and rendered.


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.


I've found the cause...

I took out the cbo.Datasource = null line.. and added a cbo.Invalidate() at the end. This has solved the problem.

Thanks all for the advice.