在VB.NET中下拉 [英] Drop Down in VB.NET
问题描述
我有一个小要求,即:
表单上有两个组合框,用于填充员工姓名和角色。我按如下所示填充组合框:
There are two combo boxes on a form and for populating the employee names and roles. I am populating the combo boxes as follows:
-
我创建了一个名为 DbConnect的类,其中有02个函数如:
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
已设计了表单有两个组合框,并将数据填充到其中:
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.
问候,
George
Regards,
George
推荐答案
您需要添加绑定源和数据关系才能使其正常工作。考虑一下遍历,它适用于datagridviews,但概念是一样。
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.
我做了一个快速的模拟来告诉您一个想法。请记住, EmpTable是您分配给数据表的名称, EmpColumn是父列,类似地,将相同的逻辑应用于角色表。代码的关键更改是两个表都必须位于具有数据关系的同一数据集中。
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
祝你好运。
这篇关于在VB.NET中下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!