Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > multiColumn Combobox
 

multiColumn Combobox

I am looking for a way to have more than one column in a combobox.  I also need a hidden value property to be the "SelectedValue".  If anyone knows how to do this it would be great.


Kevin
MigrationUser 1  Wednesday, January 14, 2004 11:12 AM
Don't know if you can have more than one column in a combo box, unless you plan on owner drawing each item.  And then each item still takes up one row, but you can just show more than once piece of data per item in a columnar format.

You can set DisplayMember and ValueMember separately.  Then the SelectedValue should return the contents of the ValueMember property while the view to the user will be the DisplayMember.
MigrationUser 1  Wednesday, January 14, 2004 3:32 PM
The only way that I've been able to simulate this, and I've used it several times in list boxes, is to create a long string containing all the fields I want to display and then include the key field far to the right so that it doesn't show in the available area.  Then when the user clicks on that item, I grab the key from the end of the text string.  This has worked very well for me.

I too wish that future versions would include combo boxes and list boxes that had the capability to display multiple columns like Access boxes do and that could be set up with a simple SQL statement.

BobSw
MigrationUser 1  Wednesday, January 21, 2004 8:40 AM
The easy way would be to do a custom control, and use listview, 

Ok basiclly you would have like a textbox a button and a listview, so normally only the textbox and button show up, you then have the listview show when you hit the button. 

It can be done, but it's not great with the current textbox, that and it still has issues with handling nulls and such. Component one has a nice combobox that does multicolumns.

MigrationUser 1  Thursday, January 22, 2004 10:43 AM
Joe,
I'm doing this now in one of my applications and got the code from the book called VB.Net 101.  It has examples for almost anything you need to do.  I don't have VS.net on this client computer so I can't post all the code but basically here's what happens.

The book contains a "Listitem" class which allows multiple columns.  In my PopulateCombo routine, it allows me to define a listitem,e.g. dim objListItem as Listitem.

This is an object which contains multiple columns of data instead of a single string  item.  The select statement could look like this:  SELECT ProductName, ProductID from products.  Run this using a datareader.  Then read the data like this:
   Do while dr.read()
       objListItem = new listitem(dr.item("ProductName").tostring(), cint(dr.item("ProductID")))
       cboProducts.items.add(objListitem)
   loop

This routine adds the listitem object containing multiple columns.  To play with the individual columns later, you could do something like the following:
   listitem = ctype(cboProducts.items, listitem)
  if listitem.ProductID = cint(ID) then
    do something
  end if

The complete code is in the book as well as the custom class.

HTH,

Ed

MigrationUser 1  Wednesday, April 14, 2004 1:55 PM
Ed,

I could not find the book you mentioned on the web and in the computer bookstores. Could you please let us know the names of the author and the publisher?

Thanks,

Andy
MigrationUser 1  Tuesday, April 27, 2004 11:50 PM
Thanks. I found the book. The name of book is 101 Microsoft Visual Basic .NET Applications published by Microsoft Press.  
MigrationUser 1  Thursday, April 29, 2004 2:07 PM
Hi,


I am using the above code, it is giving the error at Dim objListItem As ListItem.  Saying, type not defined, Does any body can give some idea. This I am using in vb.net windows forms.

Here is the sample code. When I run the above example as it is on my computer it is running fine but, my application giving me the error. Not sure, why..do I have to import something or reference something.. If any body can give me the reply on this, that would be great.



  Dim cnSQL As SqlConnection
        Dim cmSQL As SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
        Dim objListItem As ListItem
        Dim strID As String

        Try

            ' Get Primary Key from Listbox
            objListItem = CType(lstSalesCodes.SelectedItem, ListItem)


MigrationUser 1  Thursday, February 10, 2005 10:08 AM
Howdy,

You probably already figured this out, but I just finished beating my head over this same problem and thought it would be good to post what turned out to work for me.

I wanted to be able to load my combobox from a datasource...  I couldn't bind the control, because even setting SelectedIndex = -1 twice like MS suggests wasn't working for some weird reason (even though I was setting it in the load event.....)  But I really wanted the primary key from the data I was grabbing.  So, in order to get it to work in a Windows Form application (not asp.net), I had to add each item from the dataset into the control using a user class...  so enough talk.  Here's the class:


Public Class KeyValuePair
    Private m_Key As Object
    Private m_Value As Object

    Public Property Key() As Object
        Get
            Return m_Key
        End Get
        Set(ByVal Value As Object)
            m_Key = Value
        End Set
    End Property

    Public Property Value() As Object
        Get
            Return m_Value
        End Get
        Set(ByVal Value As Object)
            m_Value = Value
        End Set
    End Property

    Public Sub New(ByVal Key As Object, ByVal Value As Object)
        m_Key = Key
        m_Value = Value
    End Sub

    Public Overrides Function ToString() As String
        Return m_Value
    End Function
End Class

--

and here's the loop to load the data:

For Each dr As DataRow In dTable.Rows
    cboBox.Items.Add(New KeyValuePair(dr(0), dr(DisplayField)))
Next

cboBox.SelectedIndex = -1

--

So far it's working great for me...  now just to go through the old code and update it to work with the new class :P
MigrationUser 1  Thursday, March 24, 2005 11:37 AM
www.allplussoftware.com recently released 3 powerful data entry lookups for the .NET Framework version 1.1. Based on the DataGrid control (much powerful than the ComboBox control) these lookups take existing DataTable and can be implemented fast in as little as 4 or 5 statements. Check their site at www.allplussoftware.com
MigrationUser 1  Wednesday, April 13, 2005 3:47 PM
I'm not really seeing how that class makes a multi-column combo box. Perhaps I'm not implementing it properly, however, I've got the class built, I am calling it the same way, and it's showing me a single column of data. Am I missing something?
Kaleb  Thursday, July 21, 2005 2:18 PM

The following articles may help:

http://www.vbaccelerator.com/home/NET/Code/Controls/ListBox_and_ComboBox/Icon_ComboBox/article.asp
http://www.codeproject.com/vb/net/multicolumncombo.asp

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe Stegman  Thursday, July 21, 2005 5:49 PM
Yeah I found those links afterwards, Joe. Who would've thought that trying to make a multi-column combo box would be so difficult! I certainly didn't! I know that there are talks about bringing this into .NET 2, however, I'm stuck at 1.1 for at least awhile, so that's what I have to work with. Thank you for the reply, though. It's muchly appreciated.


On a forum note, why does it change names to WinFormUser after x amount of time?
Kaleb  Tuesday, July 26, 2005 2:16 PM

Unfortunately, this won't be in VS 2005 either; however, we are considering this for future versions of the framework.  The basic issue is we (Windows Forms) use the Windows OS provided ComboBox.  The OS provided ComboBox doesn't support this and we'd have to implement a solution on top of the Windows OS ComboBox (which is what the articles do).  Unfortunately, implementing a truly general solution on top of the Windows OS ComboBox is not an easy task (needs to correctly handle loc/RTL/accessibility/performance/scalability/etc).  Given this and the amount of other high priority work we had on our plates, we were unable to provide this in VS 2005.

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe Stegman  Tuesday, July 26, 2005 2:58 PM
hrm...my information must've been inaccurate. My apologies. I found a new multi-column combo box today that seems to work better then the other 2 that I've seen/played with:

http://www.edneeis.com/control.aspx?ID=7

Very simple to work with, and it allows for the use of datasources as well!
Kaleb  Tuesday, July 26, 2005 7:48 PM
This code below works fine

you will need to add the following line at the top before using this code

using

System.Collections;


then write down the below code anywhere you want in the form load event handler



comboBox1.Items.Clear();

ArrayList al = new ArrayList ();

al.Add(

new KeyValuePair<string, int>("HMM", 1));

al.Add(

new KeyValuePair<string, int>("G1", 2));

al.Add(

new KeyValuePair<string, int>("G2", 3));

comboBox1.DataSource = al;

comboBox1.DisplayMember =

"Key";

comboBox1.ValueMember =

"Value";

AmrGody  Saturday, July 18, 2009 3:16 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView Prevent Leaving Cell with ObjectDataSource on Errors
• How to change cell font in a DataGridView control?
• DataGrid
• Datagrid ButtonColumn
• Row comitted on datagridview
• How do I do the toal for the numbers in a single textbox? Using VB.Nt
• ADO connection to Sybase 12
• Datagrid sorted with relations
• Checkbox Checked During FullRowSelect
• EndEdit and AddNew with an ISA-relation