SQL 列到文本框(来自 ComboBox)

Posted

技术标签:

【中文标题】SQL 列到文本框(来自 ComboBox)【英文标题】:SQL Column to TextBox (from ComboBox) 【发布时间】:2019-01-30 12:22:38 【问题描述】:

我已经设法从我的 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

以上操作没有问题,所有项目都添加到组合框中,但我需要其他两列(EMAIL_ADDRESS 和 DEPARTMENT)中的相应行从组合框中选择的内容添加到文本框中。

【问题讨论】:

【参考方案1】:

在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 事件可以看到它。

【讨论】:

我认为我没有正确地做到这一点。我收到 System.IndexOutOfRangeException: 'There is no row at position 0' Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim dt As New DataTable TextBox1.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("EMAIL_ADDRESS")) TextBox3.Text = CStr(dt.Rows(ComboBox1.SelectedIndex)("DEPARTMENT")) End Sub【参考方案2】:

我建议你使用BindingSource 来完成它。

试试这个:

1- 用事件声明你的变量类型BindingSource。另外,声明您的数据集将用于数据。

Dim WithEvents BS As New BindingSource
Dim ds As New DataSet

2- 在您的 Form Load 事件中,查询您的数据并将其与绑定源和您的 ComboBox 控件绑定。

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- 添加Sub Procedure 以将您的数据显示到其他文本框控件中。

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- 在绑定源的PositionChanged 事件中,调用该子过程(如上)

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

5- 最后,要将您的数据与ComboBox 选择同步,您需要根据ComboBox 索引选择更改该绑定源的位置

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

【讨论】:

当只有一个DataTable时为什么要DataSet 既然有绑定源,为什么不绑定文本框?

以上是关于SQL 列到文本框(来自 ComboBox)的主要内容,如果未能解决你的问题,请参考以下文章

猎豹MFC--下拉框ComboBox 列表框ListBox

vb combobox控件 如何选择其中一项使一些文本框不能用,选令一项之前不能用的,变成能用

关于EasyUI ComboBox(下拉列表框)能否直接输入文本的问题,详情如图所示。

MS Access:从 ComboBox 中选择一个选项,它会自动更新其他文本框

如何将文本设置为 AjaxControlToolkit 组合框

Qt QComBox 文本框输入itemText && 文本框查找item && 本文框添加Item &&设置显示Item数量