Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / Windows Forms

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

Rate me:
Please Sign up or sign in to vote.
2.43/5 (4 votes)
23 Oct 2009CPOL1 min read 32.6K   6   3
How to add a 'select' option to a Windows Forms databound combobox with one easy method

Introduction

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

C#
public static DataTable GetComboBoxedDataTable
	(DataTable oldDataTable, string valueColumn, string textColumn, 
  	string topRowValue, string topRowText)
{
DataTable newDataTable = new DataTable();
newDataTable.Columns.Add(valueColumn);
newDataTable.Columns.Add(textColumn);

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...

C#
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.

History

  • 23rd October, 2009: Initial post

License

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


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
kailash0224-Mar-13 21:03
kailash0224-Mar-13 21:03 
GeneralMy vote of 2 Pin
Kanasz Robert27-Sep-12 10:55
professionalKanasz Robert27-Sep-12 10:55 
GeneralMy vote of 2 Pin
gaurav_verma_mca23-Oct-09 5:09
gaurav_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.