VB.NET 中的下拉菜单

发布于 2024-09-18 11:36:42 字数 1213 浏览 9 评论 0原文

我有一个小要求,那就是:

表单上有两个组合框,用于填充员工姓名和角色。我按如下方式填充组合框:

  1. 我创建了一个名为“DbConnect”的类,其中有 02 个函数:

    公共函数 getEmployees() 作为 DataTable  
            将员工 DS 调暗为新数据集  
            昏暗的员工DA作为新的SqlDataAdapter(“prc_emp_list”,conn)  
            员工DA.Fill(员工DS,“员工”)  
            返回employeeDS.Tables("employees")  
    结束功能  
    
    公共函数 getRoles() 作为 DataTable  
            Dim roleDS 作为新数据集  
            Dim roleDA As New SqlDataAdapter("prc_role_list", conn)  
            roleDA.Fill(roleDS, "角色")  
            返回角色DS.Tables(“角色”)  
    结束功能  
    
  2. 设计了一个带有两个组合框的表单,并将数据填充到其中:

    公共子员工()  
        访问函数.Open()  
        cboEmployees.DataSource = accessFunction.getEmployees  
        cboEmployees.DisplayMember = "emp_name"  
        cboEmployees.ValueMember = "login_id"  
    结束子  
    
    公共子角色()  
            访问函数.Open()  
            cboRoles.DataSource = accessFunction.getRoles  
            cboRoles.DisplayMember = "角色名称"  
            cboRoles.ValueMember = "role_id"  
    结束子  
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 MyBase.Load  
        雇员()  
        角色()  
    结束子  
    

数据正确填充到组合框中,我的要求是,当我从第一个组合中选择员工时,他的应在第二个组合中选择相应的角色。

任何人,请帮我解决这个要求。

问候,
乔治

I have a small requirement and that is:

There are two combo boxes on a form and for populating the employee names and roles. I am populating the combo boxes as follows:

  1. I have created a class called "DbConnect" and in that there are 02 functions as:

    Public Function getEmployees() As DataTable  
            Dim employeeDS As New DataSet  
            Dim employeeDA As New SqlDataAdapter("prc_emp_list", conn)  
            employeeDA.Fill(employeeDS, "employees")  
            Return employeeDS.Tables("employees")  
    End Function  
    
    Public Function getRoles() As DataTable  
            Dim roleDS As New DataSet  
            Dim roleDA As New SqlDataAdapter("prc_role_list", conn)  
            roleDA.Fill(roleDS, "roles")  
            Return roleDS.Tables("roles")  
    End Function  
    
  2. Have designed a form with two combo boxes and am populating data into them as:

    Public Sub employees()  
        accessFunction.Open()  
        cboEmployees.DataSource = accessFunction.getEmployees  
        cboEmployees.DisplayMember = "emp_name"  
        cboEmployees.ValueMember = "login_id"  
    End Sub  
    
    Public Sub roles()  
            accessFunction.Open()  
            cboRoles.DataSource = accessFunction.getRoles  
            cboRoles.DisplayMember = "role_name"  
            cboRoles.ValueMember = "role_id"  
    End Sub  
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        employees()  
        roles()  
    End Sub  
    

The data is getting populated into the combo boxes correctly and my requirement is that when I select and employee from the first combo, his corresponding role should get selected in the second combo.

Anyone, please help me on this requirement.

Regards,
George

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

篱下浅笙歌 2024-09-25 11:36:42

您需要添加绑定源和数据关系才能使其正常工作。考虑这个演练,它是针对 datagridviews 的,但概念是相同的。

我做了一个快速的模型来给你一个想法。请记住,“EmpTable”是您分配给数据表的名称,“EmpColumn”是父列,类似地将相同的逻辑应用于 Roles 表。对代码的关键更改是两个表必须位于具有数据关系的同一数据集中

Dim dtEmp as Datatable
Dim dtRole as Datatable

''//fill tables here

Dim ds as New Dataset()
ds.Tables.add(dtRole)
ds.Tables.add(dtEmp)

Dim dr as New DataRelation( _
 ds.Tables("EmpTable").Columns("EmpColumn"),
 ds.Tables("RoleTable").Columns("RoleColumn"))

''//create binding sources
Dim bsEmp as New BindingSource
Dim bsRole as New BindingSource
bsEmp.Datasource = ds
bsEmp.DataMember = "EmpTable"
bsRole.Datasource = bsEmp
bsRole.DataMeber = "RoleTable"

''//bind the binding sources to the appropriate comboboxes
cboEmployee.Datasource = bsEmp
cboRole.Datasource = bsRole

祝你好运。

You need to add a binding source and a data relationship to get this to work. Consider this walk through, it is for datagridviews but the concept is the same.

I did a quick mock up to give you an idea. Remember that "EmpTable" is the name that you assign to your datatable and "EmpColumn" is the parent column, similarly apply the same logic to the Roles table. The key change to your code is that both tables must be in the same dataset with a datarelationship.

Dim dtEmp as Datatable
Dim dtRole as Datatable

''//fill tables here

Dim ds as New Dataset()
ds.Tables.add(dtRole)
ds.Tables.add(dtEmp)

Dim dr as New DataRelation( _
 ds.Tables("EmpTable").Columns("EmpColumn"),
 ds.Tables("RoleTable").Columns("RoleColumn"))

''//create binding sources
Dim bsEmp as New BindingSource
Dim bsRole as New BindingSource
bsEmp.Datasource = ds
bsEmp.DataMember = "EmpTable"
bsRole.Datasource = bsEmp
bsRole.DataMeber = "RoleTable"

''//bind the binding sources to the appropriate comboboxes
cboEmployee.Datasource = bsEmp
cboRole.Datasource = bsRole

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文