How to modify ComboBox item collection while being databound?

softwarea used Ask the Experts™
Hi guys,

I'm trying to derive my own ComboxBox from the standard System.Windows.Forms.ComboBox. You can see part of the code below.

My ComboBox has a new property called "AddDefaultItem". If this property is set true, I would like to automatically add an item like "Please choose..." in the ComboBox' item collection when the ComboBox gets a datasource assigned.

In other words:
1. MyComboBox is bound to a BindingSource.
2. If the BindingSource is assigned to a DataSource the items collection of MyComboBox is populated.
3. If AddDefaultItem == true, I would like to add "Please choose..." to the existing items and make it top item in the collection.

To achieve this goal I believe I have to overwrite a certain event of System.Windows.Forms.ComboBox.
However: I don't know which event to take, but my guess is it must fire after the items collection has been filled already...and then I just do something like myComboBox.Items.Insert(0, "Please choose...");

Could anybody please tell me if this is the right way to go? And which event I had to overwrite?

Thank you very much!

public partial class myComboBox : System.Windows.Forms.ComboBox
    public bool AddDefaultItem { get; set; }

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

If your combobox is bound you can no longer modify its items. You need to modify the datasources items.


Hm, does not sound good :-(

But here is the thing: the datasource to be bound is generated by some business logic. And I really don't want to modify this business logic. It should always pass "real" data, but not optional stuff like "Please choose...". So, I would still prefer if MyComboxBox could do the job.

If MyComboBox really can't modify the items AFTER they were bound, maybe there is a way to modify them just BEFORE they are bound...

Any idea?

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.


Thanks for your help, Senz79. But I really don't see who any of those two links would help me here. Maybe I don't get it...
softwarea, you cannot add items to a bound combobox. As I said you will have to add items to the underlining datasource for it to work.


I'm sorry. I misunderstood you and thought there is no chance to modify the items collection AFTER the ComboBox has been bound. I was still hoping for a chance to modify the items just BEFORE or DURING the binding takes place.

So, are you saying that if I choose to use the .NET DataBinding-Feature, there is no way to influence anything about the item collection any more?

I really wouldn't know how to modify the underlying datasource because the ComboBox does not know anything about where the data comes from (and it should stay like this).

Well, I will think about something else and will post my solution here (if I find one).

Thank you very much for your help so far guys.

how are you binding in the first place?



first there is a Business Layer that delivers a List<User>. And this business layer does not know anything about the UI, e.g. the ComboBox.

The ComboBox has a BindingSource component assigned.

So, all I do in code is:

MyComboBox.DataSource = (List<User>) myUserList;

So, I only have two chances:
1. The ComboBox adds a "Please choose..." item if AddDefaultItem == true
2. I add the "Please choose..." item in the List<User> and the ComboBox has to remove it when AddDefaultItem == false

I clearly tend to option 1, because I really don't want to modify the business layer because of this.

On the other hand, if I understood you correctly, neither 1. nor 2. will work because I can't add nor remove items when I use databinding.

My very last chance would be to not use databinding, but add all items manually. However, this would be a pitty as I wouldn't be able to use the automatism of the .NET databinding features anymore.

You can just modify the list before settings the datasource...
List<User> users =  (List<User>) myUserList;

users.Add(new User([whatever constructor params]));
MyComboBox.DataSource = users;
Ok, thanks.

Besides, this really isn't a satisfying solution as the business logic has no idea if "AddDefaultItem" is set true or false. Anyway, thanks very much for you help and suggestions.

As I said: i f I find a solution or workaround I'll let you guys know.

Aren't you binding on the UI layer and not the datasource layer?


Not sure if I understood your question.

1. I have two components on my WinFrom: a ComboBox and a BindingSource component.
2. Design time setting: ComboBox.DataSource = BindingSource.
3. Run time (in code): BindingSource.DataSource = BusinessLayer.GetUserList();

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial