Results 1 to 5 of 5

Thread: Combo box with data source

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    13

    Combo box with data source

    I am trying something new with my current project. I have a combo box, and it is being populated by a bound data source and table adapter with filter. It is displaying the names of technicians as pulled from a table in the DB, but from my understanding it should also have the other columns in the data pull.

    The goal is to have it so when a technician is selected from the pull-down, the techID (different column than displayed) can by stored/placed where it needs to. For testing this is a label on the form. I have not been able to figure out exactly how to do this. When I click on the combo box, I am directed to the following in the code


    Code:
    Private Sub EMPLOYEEBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles EMPLOYEEBindingSource.CurrentChanged
    
    End Sub
    The actual control on the form has a name of cbUser. When I run the code in test it does populate the list, but trying to pull the ID is proving beyond my current knowledge.
    The DataSource for the control is set for the EMPLOYEEBindingSource
    The DisplayMember is presently set for Firstname (a valid column from the DB table)

    Any pointers or hints would be appreciated.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Combo box with data source

    Supposing you want to display the currently selected row’s ‘aField’ in TextBox1…

    TextBox1.DataBindings.Add(“Text”, your combobox’s datasource, “aField”)

    That’s all you need

    Edit: Put that code in Form_Load

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Combo box with data source

    Quote Originally Posted by .paul. View Post
    Put that code in Form_Load
    It ought to be done in the same place as the binding of the ComboBox. If the ComboBox is bound in code then the TextBox ought to be bound in code immediately after. If the ComboBox is bound in the designer then the TextBox ought to be bound in the designer too. You can achive the latter via the (DataBindings) node in the Properties window after selecting the TextBox.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    13

    Re: Combo box with data source

    Thank you, that did the trick, I didn't know it would link right, but it does. Much appreciated.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Combo box with data source

    To elaborate on .paul.'s solution, what you're doing when you setup the DataBindings is that you bind the control to the same data source as your ComboBox. Now when the selected value of the ComboBox changes, the control that is bound to the data source (in .paul.'s example, a TextBox) also gets updated.

    Generally speaking if you add your data source using the Data Sources window (shortcut key Shift + Alt + D) then you can drag and drop (or copy/paste) the fields from the window to the form and Visual Studio will setup the data bindings for you. It is pretty cool and I would highly consider you play around with it. If you wanted to see what Visual Studio sets up for you behind the scenes, then select the control that was created for you, hit the F4 key to bring up the properties window (it may already be open), and then expand the DataBindings option.

    Also, as forum etiquette: if the thread is resolved, then please mark it as such. Click on Thread Tools at the top and then Mark Thread Resolved

    Edit- Also as forum etiquette it is polite to read all of the posts prior to posting yourself. I clearly didn't do that and missed JMcIlhinney's comments so I basically reiterated what he said. I apologize for that.
    Last edited by dday9; Sep 16th, 2021 at 08:54 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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