Results 1 to 8 of 8

Thread: combo box, 2 columns [*Resolved *]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    combo box, 2 columns [*Resolved *]

    Hello,

    I have a database table with 2 fields, DepartmentCode, and DepartmentName. I want to add the values of these 2 fields into a combo box from the database. I can add one field, but how can you add 2 fields to the combox so that when the user clicks it, it will display all the departmentCode and Department name in 2 columns.

    I can do this with just one field, but l am having trouble tying to display the extra field. Is this possible in a combox box?

    Thanks in advance,

    Steve
    Last edited by steve_rm; Jun 17th, 2005 at 06:33 AM.
    steve

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: combo box, 2 columns

    Concantanate them using the & character.

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: combo box, 2 columns

    as what hack said you can use & or the + sign.
    VB Code:
    1. 'like this one
    2. combobox1.items.add("field1" + " " + "field2")

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: combo box, 2 columns

    There are multi-column ComboBoxes available on the Net. Try the code bank for this site and if there isn't one then look elsewhere. The Code Project is always a good place to start.

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: combo box, 2 columns

    Quote Originally Posted by Hack
    Concantanate them using the & character.
    you can't use the & concat method in c#. you have to use +
    You really should be concating them at the database anyway...less load on your app . like this:
    Code:
    SELECT field1 + ' ' + field2 AS BothFields FROM TABLE
    then, when you bind (i'm assuming you're binding), bind to 'BothFields'


  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: combo box, 2 columns

    Thanks everyone,

    Problem solved.
    steve

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: combo box, 2 columns [*Resolved *]

    Any insight on how you solved it? Someone else reading this thread may be interested as well.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: combo box, 2 columns [*Resolved *]

    Hello,

    Here is the full solution to the problem. Thanks for you help.

    VB Code:
    1. //Load the combo box of the department names
    2.                 cmd.CommandText = "SELECT DepartmentCode + ' ' + DepartmentName AS CodeAndName FROM Department";       
    3.                 OleDbDataReader dReaderDepartments = cmd.ExecuteReader();
    4.  
    5.                 while ( dReaderDepartments.Read() )//While there are departments to add to the combo box
    6.                 {   //Add each item from the data reader to the combo box
    7.                     cboDepartment.Items.Add( dReaderDepartments.GetString(0) );
    8.                     cboEmployeeDepartments.Items.Add ( dReaderDepartments.GetString(0) );
    9.                 }
    10.                 dReaderDepartments.Close();
    11.                 cmd.Dispose();
    steve

Posting Permissions

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



Click Here to Expand Forum to Full Width