Click here to Skip to main content
15,920,503 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 804.6K   55.8K   182   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: 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 
GeneralBug? When added a tab control Pin
jayson_1320-May-03 20:23
jayson_1320-May-03 20:23 
GeneralRe: Bug? When added a tab control Pin
JulieB21-May-03 10:27
JulieB21-May-03 10:27 
GeneralRe: Bug? When added a tab control Pin
Christian Uhlig24-Jun-03 9:34
Christian Uhlig24-Jun-03 9:34 
GeneralRe: Bug? When added a tab control Pin
Wolf4web25-Jun-03 2:16
Wolf4web25-Jun-03 2:16 
GeneralRe: Bug? When added a tab control Pin
damylen25-Mar-04 1:29
damylen25-Mar-04 1:29 
GeneralWith DataGrid on screen popup get invisible !!!!!! Pin
mneu19-May-03 4:59
mneu19-May-03 4:59 
Hi, first I think this is a cool Control.Smile | :) Thx for it. Second I played a little bit around with it and found the following issue: I have a datagrid on the same form as the multicolumn combobox. The datagrid is populated thru a backround thread (that's important). Because this Thread posts it's results 2-3 secs later than the 'load' of the dialog (and start's imediatly with a new cycle) i found the problem that the popup window disapears everytime the datagrid is populated (in addition to that the dataset is every time a new object). I faked my scenario on your example by populating the datagrid after a 5sec timer tick (same behavior). The popup seams to get hidden by the main window i believe because the visibiliy state of the popup is still true and the 'focus leave' didn't fire. Maybe you or anyone else can explain this behavior to me and how to avoid the disapperence of the popup.

Cheers

M Neu

Big Grin | :-D
GeneralRe: With DataGrid on screen popup get invisible !!!!!! Pin
tonyip5-Oct-03 18:03
tonyip5-Oct-03 18:03 
GeneralHey Jetro Thull !!! Pin
Braulio Dez6-Mar-03 23:45
Braulio Dez6-Mar-03 23:45 
GeneralGetting the Value Pin
Anonymous24-Feb-03 6:06
Anonymous24-Feb-03 6:06 
GeneralJoe Pin
Anonymous26-Nov-02 18:46
Anonymous26-Nov-02 18:46 
GeneralRe: Joe Pin
SeeSharp27-Nov-02 17:42
SeeSharp27-Nov-02 17:42 
GeneralRe: Joe Pin
Mukhaimy G.1-Feb-03 23:55
Mukhaimy G.1-Feb-03 23:55 
GeneralRe: Joe Pin
rajeshmr22-Jul-03 14:45
rajeshmr22-Jul-03 14:45 
GeneralRe: Joe Pin
r912-Oct-03 1:19
r912-Oct-03 1:19 
GeneralRe: Joe Pin
edolis20-Feb-04 2:07
edolis20-Feb-04 2:07 
GeneralSolution Pin
Spiropl7-Mar-04 23:00
Spiropl7-Mar-04 23:00 

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.