Click here to Skip to main content
11,930,788 members (51,981 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked

Data Combobox

, 27 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A custom combobox control that uses a datatable and can show multiple columns


This is an example using a custom control to display multiple columns in a combobox. It uses a DataTable as its data source and unlike a standard combobox, it can show multiple columns so you show more information. It is inherited from the System.Windows.Forms.Combobox class.


This project is based on an idea by SeeSharp in his Multi Column Combobox article. I used the same concept which was to inherit a Combobox and override the OnDropDown event to launch my form with the DataGridView. I initially was going to just make a DLL class but I hate having an EXE that requires other DLLs. I prefer to have things compiled right in. With that in mind, I wanted to make it simple to include the control in a project. So I combined my two forms into a single class.

Using the Code

You will only need two files:

  1. DataCombobox.cs, and
  2. DataCombobox.Designer.cs

Add the class file DataCombobox.cs to your project. This should also add DataCombobox.Designer.cs automatically.

Put the line...

using datacombobox;

... in your form and then rebuild your solution. This will make the custom control show up in the toolbox so you can drag it to your form.

Here is a sample piece of code on how to use it.

DataTable tbl = new DataTable();
tbl.Columns.Add("code", System.Type.GetType("System.String"));
tbl.Columns.Add("desc", System.Type.GetType("System.String"));
tbl.Columns.Add("other1", System.Type.GetType("System.String"));
tbl.Rows.Add("aaa", "the a thing", "");
tbl.Rows.Add("code2", "code #2", "");
tbl.Rows.Add("code3", "code #3", "");
tbl.Rows.Add("code4", "code #4", "just a code");
tbl.Rows.Add("widget", "some sort of device", "");
tbl.Rows.Add("code5", "code #5", "");
tbl.Rows.Add("rush", "awesome rock group", "");

//these are required
dataCombobox1.TblData = tbl;
dataCombobox1.ValueColumn = "code";

//these are optional
//by default you will see all the columns in your table
//use ColumnNameList to make it show just certain columns
//this is a pipe delimited list
dataCombobox1.ColumnNameList = "code|desc";

//use ColumnHeaderList to define the text that show in the column headers
dataCombobox1.ColumnHeaderList = "Code|Description";

//you can control the width of the columns
dataCombobox1.WidthList = "50|200";

//you can control the width and height of the grid
dataCombobox1.PopupWidth = 300;
dataCombobox1.PopupHeight = 200;

TheDataCombobox looks just like a regular Combobox until you click on the dropdown button. You can also hit the down arrow key to get the dropdown list.

Another useful property is the SearchColumn property. Normally hitting the letter keys causes a search on the ValueColumn field. But you can set the SearchColumn property and the searching takes place on that column instead. An example might be that you want to search a description instead of the actual code.

Look in the source code for a simple example project.

Points of Interest

One pitfall I discovered when working on the search by key code was that you must be careful to use the correct object. Initially I was searching through the rows of the DataTable and then using that position number to select a row in the DataGridView. But the problem with that is in order of the rows in the table may not be the same as the order in DataGridView. The user can click on a column header and change the sort order on the screen at any time. So I had to search by the rows in the DataGridView itself. You have to remember that even though a DataGridView DataSource is set to a DataTable, the row number selected in the DataGridView is not going to point to that row in the DataTable. They are two different kinds of rows.


  • 25th July, 2010: Initial version


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


About the Author

Jon McCain
Software Developer (Senior)
United States United States
I have a degree in Electrical Engineering and a degree in Computer Science. I have been working as a professional programmer since 1994 but have been messing around with computers and programming since the mid 1980's. I've worked with many languages over the years including progress,php,c,vb6 and c#. I enjoy reading science fiction and mystery/thrillers.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 12:02
mvpKanasz Robert27-Sep-12 12:02 
Questionpopup screen is not right below the combox Pin
Member 935510417-Aug-12 5:20
memberMember 935510417-Aug-12 5:20 
AnswerRe: popup screen is not right below the combox Pin
Jon McCain17-Aug-12 12:12
memberJon McCain17-Aug-12 12:12 
You could try modifying popup location. Look in the protected override void OnDropDown(EventArgs e) method. There is a line:

popup.Location = new Point(parent.Left + this.Left + 4, parent.Top + this.Bottom + this.Height + 8);

This is supposed to place it right below the parent (aka the combobox). Perhaps you can change this formula a little bit.
QuestionHow can i get the value form the second column. Pin
stony1728-Feb-12 1:28
memberstony1728-Feb-12 1:28 
AnswerRe: How can i get the value form the second column. Pin
jonmc128-Feb-12 8:35
memberjonmc128-Feb-12 8:35 
GeneralRe: How can i get the value form the second column. Pin
stony1728-Feb-12 21:12
memberstony1728-Feb-12 21:12 
GeneralRe: How can i get the value form the second column. Pin
Jon McCain1-Mar-12 5:35
memberJon McCain1-Mar-12 5:35 
GeneralRe: How can i get the value form the second column. Pin
stony171-Mar-12 19:54
memberstony171-Mar-12 19:54 
GeneralThanks Pin
YZK30-Mar-11 1:10
groupYZK30-Mar-11 1:10 
GeneralMy vote of 4 Pin
John Brett28-Jul-10 1:59
memberJohn Brett28-Jul-10 1:59 
GeneralGood Work Pin
Khaniya27-Jul-10 20:45
memberKhaniya27-Jul-10 20:45 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 27 Jul 2010
Article Copyright 2010 by Jon McCain
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid