I have managed to add data into a ComboBox from a column out of my SQL table, but I need the rows from across to display in the rest of the textboxes. (I hope I have worded this correctly).
Here is my code currently:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim con As New SqlConnection("Data Source=xxxx;Initial Catalog=ltLeavers;Integrated Security=True")
Dim da As New SqlDataAdapter("SELECT * FROM dbo.mytable", con)
Dim dt As New DataTable
da.Fill(dt)
ComboBox1.DisplayMember = "DISPLAY_NAME"
ComboBox1.DataSource = dt
End Sub
The above works with no issues, all of the items add into the ComboBox but I need the corresponding rows from the other two columns which are EMAIL_ADDRESS and DEPARTMENT to add into TextBoxes from what is selected in the ComboBox.
I suggest you to use
BindingSource
to get it done.Try this:
1- Declare your variable type of
BindingSource
with events. Also, declare your dataset will be used for data.2- In your
Form
Load
Event, query your data and bind it with both Binding Source and yourComboBox
control.3- Add a
Sub Procedure
to display your data into other text boxes controls.4- In the
PositionChanged
event of your Binding Source, call that sub procedure (above)5- Finally, to sync your data with
ComboBox
selection, you need to change the position of that Binding Source according to theComboBox
index selectionUnder the
SelectedIndex_Changed
event of the ComboBox; Enter the following code.You Will need to declare the data table dt outside your form's load event so it can be visible to the SelectedIndex_Changed event of the combo box.