CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2011

    Unhappy Show multiple column from database in a combobox

    I am a newbie in C#. How can I show two columns from a database in a DropDownList (ComboBox)?

    Currently I can show one column from my database.

    cbBox1.DataSource = dba.generateNameList();
    cbBox1.DisplayMember = "Name";

    /* Here dba.generateNameList() returns an ArrayList which holds the data of my database
    This way I could show one column in my cbBox1 successfully.

    I am trying to show "ID" from my database along with "Name" Like this "Johny(1001)"
    I tried it this ways
    cbBox1.DisplayMember = "Name" + "(ID)";
    But, it shows compiler error.

    Somebody please help me.

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    Re: Show multiple column from database in a combobox

    Not sure if the itemdata property is still on the combobox in .net framework or not. In VB6 I would set the itemdata property to the ID number [numeric only] of course the id wouldbe hidden in that case.

    You can of course concantonate the name and ID together if you want them both displayed. Based on what you have given though I can't really tell you want you would need to do. I can tell you that the () around ID inside quotes will not work and in the case of displaymemeber it is looking for the name of the member to display if the case above it would try for a memeber named Name(ID) which most likely does not exist.

    You can create a sub which will read the records from the db cocantonate the name field + "(" + id field +")" and use the additem method to add the newly created string to your combo box to get the display you seek.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2011

    Re: Show multiple column from database in a combobox

    Thanks for your reply.

    I successfully did it in another way.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Show multiple column from database in a combobox

    If you are using WPF, you can style the combobox template to add columns to the dropdown. That way everything will appear nicely formatted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)