SQL Column to TextBox (from ComboBox)

2019-03-01 00:20发布

问题:

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.

回答1:

Under the SelectedIndex_Changed event of the ComboBox; Enter the following code.

Dim dt As New DataTable
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)
    da.Fill(dt)
    ComboBox1.DisplayMember = "DISPLAY_NAME"
    ComboBox1.DataSource = dt

End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Textbox1.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("EMAIL_ADDRESS"))
    Textbox2.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("DEPARTMENT"))
End Sub

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.



回答2:

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.

Dim WithEvents BS As New BindingSource
Dim ds As New DataSet

2- In your Form Load Event, query your data and bind it with both Binding Source and your ComboBox control.

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)
   da.Fill(ds, "myPopulatedTable")

   ComboBox1.DisplayMember = "id"
   ComboBox1.DataSource = ds.Tables("myPopulatedTable")

   'Here the new code'
   BS.DataSource = ds
   BS.DataMember = "myPopulatedTable"

End Sub

3- Add a Sub Procedure to display your data into other text boxes controls.

Private Sub DISPLAYRECORD(Optional ByVal table As String = "myPopulatedTable")
        TextBox1.Text = ds.Tables(table).Rows(Me.BS.Position)("column1").ToString
        TextBox2.Text = ds.Tables(table).Rows(Me.BS.Position)("column2").ToString()
        TextBox2.Text = ds.Tables(table).Rows(Me.BS.Position)("column3").ToString()
End Sub

4- In the PositionChanged event of your Binding Source, call that sub procedure (above)

Private Sub BS_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BS.PositionChanged
        DISPLAYRECORD()
 End Sub

5- Finally, to sync your data with ComboBox selection, you need to change the position of that Binding Source according to the ComboBox index selection

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        BS.Position = ComboBox1.SelectedIndex
End Sub