Click here to Skip to main content
15,889,462 members
Articles / Programming Languages / C#
Article

Multi Column ComboBox

Rate me:
Please Sign up or sign in to vote.
4.63/5 (56 votes)
17 Nov 2002 801.9K   55.7K   181   92
Displaying Multiple Columns in a Dropdown Combobox

Introduction

This is a simple example of how to display table values (i.e. multiple columns) in a combo box. For the dropdown window, a form with a ListView docked to fill can be used. The MultiColumnCombobox class is inherited from the System.Windows.Forms.Combobox class.

C#
protected override void OnDropDown(System.EventArgs e){
    Form parent = this.FindForm();
    if(this.dataTable != null || this.dataRows!= null){
        MultiColumnComboPopup popup = new 
                            MultiColumnComboPopup(this.dataTable,
                            ref this.selectedRow,columnsToDisplay);
        popup.AfterRowSelectEvent+=
                            new AfterRowSelectEventHandler
                            (MultiColumnComboBox_AfterSelectEvent);
        popup.Location = new Point(parent.Left + 
                            this.Left + 4 ,parent.Top + 
                            this.Bottom + this.Height);
        popup.Show();
        ..........................

The OnDropDown event of the ComboBox is overridden to display our popup from. The popup form should never be a modal, because the popup has to close, if the user decides to click elsewhere other than the grid on the dropdown form. To check where the user has selected something, I use the popup.AfterRowSelectEvent, to fire an event on the MulticolumnComboBox. To use the MulticolumnComboBox do the following:

multiColumnComboBox1.Table = dtable;//DataTable
//Column to display after selection
multiColumnComboBox1.DisplayMember = "Band";
multiColumnComboBox1.ColumnsToDisplay = new 
    string[]{"Band","Song","Album"};//columns to display
//in the dropdown grid

After the DataTable is assigned to the combo, the dropdown looks like this:

After Selection, the DisplayMember of that row is displayed.

The SourceCode is pretty much self-explanatory, download it and try it out.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Deva Anbu Daniel is an Application Developer with 2+ years experience in Java,VB and C#. Currently working in Insoft.com(www.insoft.com). His favorite language is C. He's a major fan of Iron Maiden and Metallica and Jethro Tull.

Comments and Discussions

 
Generalmulty column DropDownlist Pin
saeednaseh10-Aug-04 4:18
saeednaseh10-Aug-04 4:18 
GeneralRe: multy column DropDownlist Pin
Kevin Cochran15-Sep-04 8:49
sussKevin Cochran15-Sep-04 8:49 
GeneralFIX: selectedrow returns nothing Pin
Stefan7510-Jun-04 23:23
Stefan7510-Jun-04 23:23 
QuestionRe: FIX: selectedrow returns nothing Pin
maniatisa27-Sep-06 2:46
maniatisa27-Sep-06 2:46 
GeneralDropDrownList Style Pin
code300512-Apr-04 10:46
code300512-Apr-04 10:46 
GeneralGet or Set SelectedValue Pin
code30057-Apr-04 7:31
code30057-Apr-04 7:31 
GeneralRe: Get or Set SelectedValue Pin
DougW487-Apr-04 22:04
DougW487-Apr-04 22:04 
GeneralRe: Get or Set SelectedValue Pin
sacp.net14-May-07 12:10
sacp.net14-May-07 12:10 
Hi,

Can you please send me the updated code on pisal_sachin@yahoo.com

thanks a lots.

Regards,
sac
GeneralRe: Get or Set SelectedValue Pin
javadpishvaei14-Oct-07 1:37
javadpishvaei14-Oct-07 1:37 
GeneralRe: Get or Set SelectedValue Pin
Member 406020411-Apr-08 5:42
Member 406020411-Apr-08 5:42 
QuestionRe: Get or Set SelectedValue Pin
daouadji1-Mar-07 5:29
daouadji1-Mar-07 5:29 
Generalselect on single click Pin
lewisv30-Mar-04 4:06
lewisv30-Mar-04 4:06 
GeneralRe: select on single click Pin
DougW487-Apr-04 3:10
DougW487-Apr-04 3:10 
GeneralRe: select on single click Pin
lewisv7-Apr-04 3:38
lewisv7-Apr-04 3:38 
GeneralRe: select on single click Pin
DougW487-Apr-04 3:57
DougW487-Apr-04 3:57 
GeneralRe: select on single click Pin
Thomas Sauder11-Jun-04 4:55
Thomas Sauder11-Jun-04 4:55 
GeneralRe: select on single click Pin
Thomas Sauder11-Jun-04 5:04
Thomas Sauder11-Jun-04 5:04 
QuestionHow to tune hover delay? Pin
edolis20-Feb-04 1:57
edolis20-Feb-04 1:57 
GeneralCombobox code in vb.net Pin
sasa123415-Feb-04 22:33
susssasa123415-Feb-04 22:33 
GeneralCombobox code in vb.net Pin
SallyKaps12315-Feb-04 22:33
sussSallyKaps12315-Feb-04 22:33 
GeneralRe: Combobox code in vb.net Pin
Hanan Nachmany27-Mar-04 20:16
Hanan Nachmany27-Mar-04 20:16 
GeneralProblem with Column Width Pin
drkelly9-Oct-03 11:00
drkelly9-Oct-03 11:00 
GeneralRe: Problem with Column Width Pin
Gmonkey28-Oct-03 7:21
Gmonkey28-Oct-03 7:21 
GeneralRe: Problem with Column Width Pin
Michael Hawksworth7-Apr-04 0:26
Michael Hawksworth7-Apr-04 0:26 
GeneralRe: Problem with Column Width Pin
tysonx16-Mar-05 1:29
tysonx16-Mar-05 1:29 

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.