gpt4 book ai didi

SQL 列到 TextBox(来自 ComboBox)

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:55 25 4
gpt4 key购买 nike

我已经设法将数据从我的 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 中选择的内容添加到 TextBox 中。

最佳答案

ComboBox的SelectedIndex_Changed事件下;输入以下代码。

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

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

关于SQL 列到 TextBox(来自 ComboBox),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54440570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com