Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 23 Oct 2009
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

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

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

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)

Share

About the Author

greg_quinn

South Africa South Africa
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberkailash0224-Mar-13 21:03 
GeneralMy vote of 2 PinmvpKanasz Robert27-Sep-12 10:55 
GeneralMy vote of 2 Pinmembergaurav_verma_mca23-Oct-09 5:09 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 23 Oct 2009
Article Copyright 2009 by greg_quinn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid