如何填充基于另一个ComboBox使用连接字符串(SQL Server,VB.NET)的ComboBox, [英] How to populate a ComboBox based on another ComboBox using a connection string (SQL Server, VB.NET)

查看:81
本文介绍了如何填充基于另一个ComboBox使用连接字符串(SQL Server,VB.NET)的ComboBox,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从combobox 1(我们与之合作的公司)选择一个客户端时,它应该仅从该客户端的组合框2中填充用户,这需要来自SQL Server中的特定数据库。



示例:



当用户选择客户端时,第一个Combobox包含所有客户端,第二个ComboBox所有来自该数据库的用户,我希望第二个ComboBox的用户列表根据从第一个ComboBox的列表中选择的客户端更改,但包括连接字符串,通过SQL Server。



所以如果我在Combobox 1和Combobox 2中选择说.... Google,我期望来自Google的10个用户。但是如果我改变主意,在Combobox 1和Combobox 2中选择Yahoo,我希望这次约有12个用户来自雅虎。



数据库名称我正在使用: MyDatabase






  • GoogleQA(如果用户选择了)

  • YahooQA(如果用户选择Yahoo,只从此数据库获取用户,并在运行时填充)



我的VB.Net代码:

  Sub Form1_Load(sender As Object,e As EventArgs)句柄MyBase.Load 
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings(conStr)。ConnectionString)
Dim cmd As New SqlCommand(Select * from CLIENTS,con)
con.Open()
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())
con.Close()
cboClient.DataSource = dt
cboClient.DisplayMember =CLIENT_NAME
cboClient.ValueMember =ID

rtfMessage.Tag =在此处输入您的消息
rtfMessage .Text = CStr(rtfMessage.Tag)

我可以在我的组合框代码中我想要发生的目标:

  Private Sub cboUser_SelectedValueChanged(sender As Object,e as EventArgs)Handles cboUser.SelectedValueChanged 
- 我在这里放什么?
End Sub

Private Sub cboClient_SelectedIndexChanged(sender As Object,e As EventArgs)句柄cboClient.SelectedIndexChanged
Dim con As新的SqlConnection(ConfigurationManager.ConnectionStrings(conStr2)。ConnectionString )
Dim cmd As New SqlCommand(Select * from USERS,con)
con.Open()
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader ))
con.Close()
cboClient.DataSource = dt
cboClient.DisplayMember =NetworkID
cboClient.ValueMember =ID
cboClient.Text =
如果cboClient.SelectedItem.Text =Sunoco然后
cboUser.Items.Add(NetworkID)
如果
结束End Sub

可能性:在客户端更改组合框函数(cboClient_SelectedValueChanged)

  1。)更改数据库连接
2.)从正确的数据库中查询正确的用户数据
3.)用用户数据填充用户组合框


解决方案

您只需在if语句的load部分做

 `如果cboClient.SelectedItem.Text =Googlethen 
(使用google信息填充其他组合框的代码)
结束if`


`如果cboClient.SelectedItem。 Text =Yahoothen
dim sql as string =Select firstName ++ lastname as empName from
Yahoo表
dim cmd as sqladapter(sql,con)
dim dt as datatable
cmd.fill(dt)
cboUsers.datasource = dt
cboUsers.Datatextfield =empName
cboUsers.Datavaluefield =empName
cboUsers。 DataBind()
如果
结束

Dim sql2 As String =选择EQPnumber,EQPdesc从CREW_LINEUP_ACTIVE_EQUIPMENT
Dim activeEQPadt作为新的SqlDataAdapter(sql2,IPMS.Settings。 conn)
activeEQPadt.Fill(activeDT)
ActiveEQPLstBx.DataSource = activeDT
ActiveEQPLstBx.DataTextField =EQPdesc
ActiveEQPLstBx.DataValueField =EQPnumber
ActiveEQPLstBx.DataBind ()


When a user selects a client from combobox 1 (a company we do work with and are partners with), its supposed to populate the users from combobox 2 for that client only, which needs to come from that particular database in SQL Server.

Example:

The first Combobox with all the Clients when a user selects a client and the Second ComboBox with all the Users from that database only, I want the Users list of the Second ComboBox to change according to the Clients selected from the list of the first ComboBox, but including a connection string, via SQL Server.

So if I select say....Google in Combobox 1 and in Combobox 2, I expect say.....10 users from Google. But if I change my mind and select Yahoo in Combobox 1 and in Combobox 2, I expect say..... this time around 12 users from Yahoo.

Database Name I'm using: MyDatabase

Get the users from these databases, based on the selection I make, which contains those particular users, not MyDatabase which has users as well:

  • GoogleQA (If user selected Google, get the users from this database only and populate it during runtime)
  • YahooQA (If user selected Yahoo, get the users from this database only and populate it during runtime)

My VB.Net code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
        Dim cmd As New SqlCommand("Select * from CLIENTS", con)
        con.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        con.Close()
        cboClient.DataSource = dt
        cboClient.DisplayMember = "CLIENT_NAME"
        cboClient.ValueMember = "ID"

        rtfMessage.Tag = "Enter your message here"
        rtfMessage.Text = CStr(rtfMessage.Tag)

What can I put in my comboboxes code to get the end goal of what I want to happen:

  Private Sub cboUser_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboUser.SelectedValueChanged
--What do I put here?
    End Sub

Private Sub cboClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClient.SelectedIndexChanged
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr2").ConnectionString)
        Dim cmd As New SqlCommand("Select * from USERS", con)
        con.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        con.Close()
        cboClient.DataSource = dt
        cboClient.DisplayMember = "NetworkID"
        cboClient.ValueMember = "ID"
        cboClient.Text = ""
        If cboClient.SelectedItem.Text = "Sunoco" Then
            cboUser.Items.Add("NetworkID")
        End If
    End Sub

Possibility: On client change combo box function (cboClient_SelectedValueChanged)

1.) Change the database connection
2.) Query the proper user data from the correct database
3.) populate the user combo box with that user data

解决方案

You just do what you have done in the load section in an if statement inside your user section.

sort of like :

    `If cboClient.SelectedItem.Text = "Google" then
     (code to populate the other combobox with google info)
     End if`


     `If cboClient.SelectedItem.Text = "Yahoo" then 
        dim sql as string = "Select firstName+ " " +lastname as empName from 
        Yahoo table"
        dim cmd as sqladapter(sql, con)
        dim dt as datatable
        cmd.fill(dt)
        cboUsers.datasource = dt
        cboUsers.Datatextfield = "empName"
        cboUsers.Datavaluefield = "empName"
        cboUsers.DataBind()
     End if


        Dim sql2 As String = "Select EQPnumber, EQPdesc from CREW_LINEUP_ACTIVE_EQUIPMENT"
        Dim activeEQPadt As New SqlDataAdapter(sql2, IPMS.Settings.conn)
        activeEQPadt.Fill(activeDT)
        ActiveEQPLstBx.DataSource = activeDT
        ActiveEQPLstBx.DataTextField = "EQPdesc"
        ActiveEQPLstBx.DataValueField = "EQPnumber"
        ActiveEQPLstBx.DataBind()

这篇关于如何填充基于另一个ComboBox使用连接字符串(SQL Server,VB.NET)的ComboBox,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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