Drop Down in VB.NET

后端 未结 1 1426
终归单人心
终归单人心 2021-01-28 04:07

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条回答
  • 2021-01-28 04:42

    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.

    0 讨论(0)
提交回复
热议问题