Click here to Skip to main content
15,886,075 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.5K   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

 
GeneralRe: Bug in Control with mdichild form (simpler fix) Pin
bonghi25-May-07 3:25
bonghi25-May-07 3:25 
GeneralFlicker of Standard ComboBox dropdown still appears Pin
nathan88819-Jun-05 18:46
nathan88819-Jun-05 18:46 
GeneralRe: Flicker of Standard ComboBox dropdown still appears Pin
tjackman6-Oct-06 4:43
tjackman6-Oct-06 4:43 
GeneralRe: Flicker of Standard ComboBox dropdown still appears Pin
Nauja9-Oct-09 16:41
Nauja9-Oct-09 16:41 
Generaluse MultiColumnComboBox.ValueMember Pin
kalaj20-Feb-05 12:06
susskalaj20-Feb-05 12:06 
QuestionHow to get and set value ?? Pin
Inquire2you23-Sep-04 7:08
Inquire2you23-Sep-04 7:08 
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 
Check out:

http://blogs.madtechnology.net/chris/archive/2003/10/23/255.aspx[^]

Regards,
Kevin
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 
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 

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.