SQL列到TextBox(来自ComboBox) [英] SQL Column to TextBox (from ComboBox)

查看:57
本文介绍了SQL列到TextBox(来自ComboBox)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

这是我当前的代码:

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中。

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.

推荐答案

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

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

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

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.

这篇关于SQL列到TextBox(来自ComboBox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆