-
Sep 15th, 2021, 12:54 PM
#1
Thread Starter
New Member
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.
-
Sep 15th, 2021, 04:13 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 15th, 2021, 07:55 PM
#3
Re: Combo box with data source
Originally Posted by .paul.
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.
-
Sep 16th, 2021, 08:24 AM
#4
Thread Starter
New Member
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.
-
Sep 16th, 2021, 08:51 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|