How to bind 2 fields into 1 combobox


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 15

Thread: How to bind 2 fields into 1 combobox

  1. #1
    Join Date
    Feb 2008
    Posts
    28

    How to bind 2 fields into 1 combobox

    Hi,

    I need advice and help. I am trying to display 2 fields as a choice in 1 combobox. I only know how to display 1 field in the combobox.

    I am currently working with this code.

    CustomerSupplierIDComboBox.DataSource = E2_PRODataSet.Tables("CUSTOMER")
    CustomerSupplierIDComboBox.DisplayMember = "CustomerID"
    CustomerSupplierLabel.DataBindings.Add("Text" CUSTOMERBindingSource, "CustomerName")

    My next table to be include in this combobox is the SUPPLIER Table with the label to be display is SupplierName. How do I include all this to operate the combobox?

    Please advice.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    I would suggest two things:

    1. If you want more than one field don't use data binding. Populate your control through programming code over you which you will have more control.

    2. If you want more than one field switch to something like a grid or a Listview.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    You could generate a dataset that does a UNION between the two tables and use that as your datasource.

    ...joe

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    But still, Joe, wouldn't you agree that storing more than column of data in a garden variety combo box is not the best design idea?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    I agree 100%. It makes more sense from a design standpoint to use a customer and a supplier combobox.

    ...joe

  6. #6
    Join Date
    Feb 2008
    Posts
    28
    thanks for the advice...

  7. #7
    Join Date
    Mar 2008
    Posts
    1
    You could append two columns. for eg. if you have to display empolyer Id and employer name in the combobox you could add the lineItem like
    '1 - ABC Corp'. So you end up getting two columns seperated by a '-'

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    But, that is still a single string, not two separate strings. To get the Id or get the name, you would need to parse it. It is much simpler to simply use a control that supports columns.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  9. #9
    Join Date
    Dec 2011
    Posts
    1
    I know this is pretty much a dead thread, but it isn't really answered. So here's an idea for anyone looking at this trying to find answers. You could always make an extra column with a concatenation of the ID and Customer Name. I'm not sure how to code that, but if you don't mind using a dataset.designer go and add a column. Under the new column's properties, go to expression. Enter:

    [CustomerID] + ' ' + [CustomerName]

    into the expression field and you now have a field to pull into your combobox to show both, the customer id and customer name as the display member. Remember to set your value member as either the customer id or the customer name to put off the value you are wanting. You can't use you concatenated field for the value.

  10. #10
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    But still, in order to get the Id or get the name, you would need to parse it as it is still one string.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  11. #11
    Join Date
    Oct 2008
    Posts
    142
    hi there,

    this solution is already mentioned.
    in the select statement used to populate your combobox, you should concatenate your two columns into one column, and then use this concatenated column to display in your comboxbox.
    so, you have two columns col1 and col2, your sql statement should look like this:

    select t1.col1 + ' ' + t2.col2 as concatenated_column
    from table1 t1, table2 t2
    where...

    for your combobox, you would use your "concatenated_column".

    that's it,

    tonci korsano

  12. #12
    Join Date
    Jan 2012
    Posts
    1
    Quote Originally Posted by tkorsano View Post
    hi there,

    this solution is already mentioned.
    in the select statement used to populate your combobox, you should concatenate your two columns into one column, and then use this concatenated column to display in your comboxbox.
    so, you have two columns col1 and col2, your sql statement should look like this:

    select t1.col1 + ' ' + t2.col2 as concatenated_column
    from table1 t1, table2 t2
    where...

    for your combobox, you would use your "concatenated_column".

    that's it,

    tonci korsano
    Yes, I agree with you, that's right!

  13. #13
    Join Date
    Jan 2012
    Posts
    1
    Thanks

  14. #14
    Join Date
    Jun 2008
    Posts
    5
    // this will create a combo box with two columns.
    dtDataTable = getDataTable();
    foreach (DataRow dr in dtDataTable.Rows)
    {
    sId = dr["id"].ToString ();
    sDesc = dr["description"].ToString();
    cboBox.Items.Add(sId + " - " + sDesc);
    }

  15. #15
    Join Date
    Jul 2012
    Posts
    2
    As we know Combo Box control can only display one column(field) with the data source, it's not support by combo box that display multi column connections like field1-field2 when it is data binded. Although this cannot be done directly, we can do it indirectly by using Format event of the Combo Box. The Format event is raised before each visible item is formatted.
    http://www.dapfor.com/en/net-suite/n...l/data-binding

Similar Threads

  1. Replies: 6
    Last Post: 09-06-2013, 03:44 AM
  2. VS2005 combobox and databinding issues
    By subrama6 in forum .NET
    Replies: 4
    Last Post: 02-13-2008, 09:19 AM
  3. refreshing form fields
    By ashiers in forum Java
    Replies: 1
    Last Post: 06-19-2005, 05:09 PM
  4. comboBox question
    By Stan Shankman in forum .NET
    Replies: 2
    Last Post: 09-07-2001, 03:06 AM
  5. Adding fields into recordset
    By Karel Semerák in forum VB Classic
    Replies: 1
    Last Post: 03-27-2000, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center