Click here to Skip to main content
12,943,367 members (65,947 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 23 Oct 2009

Adding a 'select' Option to a Windows Forms Databound Combobox - The Easy Way

, 23 Oct 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to add a 'select' option to a Windows Forms databound combobox with one easy method


A common task for many developers is to insert an item at the beginning of a combobox, such as 'Select an option', or something similar.

The problem with this option in Windows Forms is that you cannot simply add this new item to a bound combobox. The new item needs to be added to the datasource the combobox is bound to.

I have created a helper method that accepts your existing datatable, takes in a few arguments, and spits out datatable with your newly added value. You then bind your combobox to this new datatable.

Let's look at some code to make this clearer...

Using the Code

public static DataTable GetComboBoxedDataTable
	(DataTable oldDataTable, string valueColumn, string textColumn, 
  	string topRowValue, string topRowText)
DataTable newDataTable = new DataTable();

foreach (DataRow oldDR in oldDataTable.Rows)
  DataRow newDR = newDataTable.NewRow();
  newDR[0] = oldDR[valueColumn].ToString();
  newDR[1] = oldDR[textColumn].ToString();
  newDataTable.Rows.InsertAt(newDR, newDataTable.Rows.Count);

// Add your 'Select an item' option at the top
DataRow dr = newDataTable.NewRow();
dr[0] = topRowValue;
dr[1] = topRowText;
newDataTable.Rows.InsertAt(dr, 0);

return newDataTable; 

This method takes 5 arguments:

  • oldDataTable - This is your datatable you have already bound to from your database.
  • valueColumn - This is the name of the column in your datatable that you bind to the ValueMember field of your combobox.
  • textColumn - This is the name of the column in your datatable that you bind to the DisplayMember field of your combobox.
  • topRowValue - This is the value of your 'Select' option you are adding to the combobox.
  • topRowText - This is the displayed text of your 'Select' option you are adding to the combobox.

The nice thing about this helper method is that it doesn't matter how many columns are in your original datatable. It only strips out the columns you need for your combobox as the standard combobox only supports two columns.

To use the method, here is an example...

DataSet ds = GetDataSetFromMyDatabase();
comboBox.DataSource = GetComboBoxedDataTable(ds.Tables[0], 
	"ID", "EmployeeName", "0", "All Employees");

So there it is, a helper method to allow you to easily add a 'Select' option into a bound combobox.


  • 23rd October, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
kailash0224-Mar-13 21:03
memberkailash0224-Mar-13 21:03 
GeneralMy vote of 2 Pin
Kanasz Robert27-Sep-12 10:55
mvpKanasz Robert27-Sep-12 10:55 
GeneralMy vote of 2 Pin
gaurav_verma_mca23-Oct-09 5:09
membergaurav_verma_mca23-Oct-09 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 23 Oct 2009
Article Copyright 2009 by greg_quinn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid