admin

SQL列到TextBox(来自ComboBox)

sql

我设法将数据从SQL表中的列添加到ComboBox中,但是我需要从头到尾的行才能显示在其余文本框中。(我希望我的措词正确)。

这是我目前的代码:

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

上面的方法没有问题,所有项目都添加到了ComboBox中,但是我需要其他两列的相应行(即EMAIL_ADDRESS和DEPARTMENT)从ComboBox中选择的内容添加到TextBoxes中。


阅读 205

收藏
2021-07-01

共1个答案

admin

SelectedIndex_ChangedComboBox事件下;输入以下代码。

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

您将需要在表单的load事件之外声明数据表dt,以便组合框的SelectedIndex_Changed事件可以看到它。

2021-07-01