Click here to Skip to main content
15,881,803 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.1K   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

 
QuestionSelectedIndex remain -1 Pin
Member 1560194714-Apr-22 0:34
Member 1560194714-Apr-22 0:34 
Questioneach column width changed to dynamic?? Pin
MaKhaing2-Aug-15 22:48
MaKhaing2-Aug-15 22:48 
Questionmulti column combo Pin
Member 1145351117-Feb-15 9:37
Member 1145351117-Feb-15 9:37 
AnswerRe: multi column combo Pin
IssaharNoam24-May-15 10:08
IssaharNoam24-May-15 10:08 
QuestionFor selected index changed/ row changed. Pin
joshua89m24-Sep-14 3:45
joshua89m24-Sep-14 3:45 
AnswerRe: For selected index changed/ row changed. Pin
Member 951370330-Jan-17 21:06
Member 951370330-Jan-17 21:06 
Questionhow to change so it positions with nested containers Pin
friedmad29-Apr-14 4:46
friedmad29-Apr-14 4:46 
AnswerRe: how to change so it positions with nested containers Pin
Coolzero200324-Oct-18 23:07
Coolzero200324-Oct-18 23:07 
QuestionUnable to download files Pin
Wanda Russell8-Nov-13 4:20
Wanda Russell8-Nov-13 4:20 
QuestionUnable to download the source code / Demo zip file Pin
Abhijeet Shastry12-Jul-13 2:20
Abhijeet Shastry12-Jul-13 2:20 
QuestionCan you make this select the item on Single Click Pin
Manu Kuriachen26-Jun-13 19:38
Manu Kuriachen26-Jun-13 19:38 
AnswerRe: Can you make this select the item on Single Click Pin
Manu Kuriachen26-Jun-13 19:56
Manu Kuriachen26-Jun-13 19:56 
QuestionSelectedValue Pin
codeder0017-Jun-13 14:57
codeder0017-Jun-13 14:57 
QuestionWhy I can not download this file? Pin
3784722807-Mar-13 19:35
3784722807-Mar-13 19:35 
Suggestiondon't show the dropdown from the original combobox Pin
andre4710-Sep-12 17:25
andre4710-Sep-12 17:25 
GeneralMy vote of 4 Pin
PioneerInfo16-Feb-12 19:09
PioneerInfo16-Feb-12 19:09 
QuestionMulticolumn combobox Pin
Hardz22-Aug-11 22:24
Hardz22-Aug-11 22:24 
GeneralMulticolumn Combobox Pin
Bryan Duchesne30-May-11 8:08
Bryan Duchesne30-May-11 8:08 
QuestionMultiColumnComboBox1_SelectedIndexChanged is not worked Pin
luyogyi22-Apr-11 8:33
luyogyi22-Apr-11 8:33 
GeneralSelectedValue Pin
fariborz sadighi13-Apr-11 21:25
fariborz sadighi13-Apr-11 21:25 
GeneralRe: SelectedValue Pin
Member 35985518-May-12 0:33
Member 35985518-May-12 0:33 
GeneralRe: SelectedValue Pin
codeder0017-Jun-13 9:45
codeder0017-Jun-13 9:45 
QuestionHow do I make this searcheable Pin
BastaNL7-Apr-11 3:22
BastaNL7-Apr-11 3:22 
QuestionMultiColumnComboBox1_SelectedIndexChanged and MultiColumnComboBox1_SelectedValueChanged not working Pin
kannan_k718-Mar-11 9:43
kannan_k718-Mar-11 9:43 
AnswerRe: MultiColumnComboBox1_SelectedIndexChanged and MultiColumnComboBox1_SelectedValueChanged not working Pin
thatraja26-Jan-12 21:16
professionalthatraja26-Jan-12 21:16 

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.