Monday, January 09, 2006

Add a first blank item in ComboBox

The built-in ComboBox Windows Forms control is often used but it has a drawback in the sense that you cannot add an extra blank item automatically.

This is quite painful as you often like to add an item like "Select one item".

I have created a control BlankItemComboBox, which does this.
It works with DataTable, DataView and Collections. It currently does not support Arrays of objects.

You can download the source code here

NOTE:
There is one drawback which is that you must set the control properties before you bind the datasource.

blankItemComboBox1.BlankText = "Select";
blankItemComboBox1.BlankValue = "0";
blankItemComboBox1.ValueMember = "ID";
blankItemComboBox1.DisplayMember = "Name";
blankItemComboBox1.DataSource = ds.Tables[0];