我正在从数据库加载人员数据并按如下方式填充数据集:
sql = " select * from personnel "
daAdapter = New SqlDataAdapter(sql, sqlConn)
daAdapter.Fill(dsDataset, "personnel ")
dgvPersonnel.datasource=dsDataset.Tables("personnel ")
sql = " select depId, name from department "
daAdapter = New SqlDataAdapter(sql, sqlConn)
daAdapter.Fill(dsDataset, "department")
'Set department combobox datasource for filtering
cmbDepartment.DataSource = dsDataset.Tables("department")
cmbDepartment.DisplayMember = "name"
cmbDepartment.ValueMember = "depId"
'Set datasource for personnel datagridview
dgvPersonnel.datasource=dsDataset.Tables("department")
现在,当我双击dgvPersonnel中的任何人员时,我加载了另一个表单“frmPersonnelDatails”,以便能够修改人员数据,我也有一个组合框来选择人员部门。
我现在关注的是如何将“frmPersonnelDatails”表单中组合框的数据源设置为我的数据集中的department表,而不是再次查询数据库以加载。意思是我不想多次查询数据。我尝试了以下但无法得到结果:
Try
serviceNo= dgvPersonnel.Rows(dgvPersonnel.CurrentRow.Index).Cells(0).Value
Dim form As New frmPersonnelDatails
personnelMode = "modify"
form.cmbDepartment.DataSource = dsDataset.Tables("department")
form.cmbDepartment.DisplayMember = "name"
form.cmbDepartment.ValueMember = "depId"
form.ShowDialog()
Catch ex As Exception
MsgBox(Err.Description)
End Try
答案 0 :(得分:0)
要以其他形式设置组合框的数据源,请在构造函数中发送此DataSet
...
以下是一个例子:
Dim fPD As frmPersonnelDatails = New frmPersonnelDatails('YOUR DS') 'Pass your dataset in the constructor'
fPD.ShowDialog()
在你的班级中:frmPersonnelDatails确保有一个新的构造函数......
Sub New(ByVal dSet As DataSet)
cmbDepartment.DataSource = dSet.Tables("department")
cmbDepartment.DisplayMember = "name"
cmbDepartment.ValueMember = "depId"
End Sub
另一种选择是在你的类中声明一个新的DataSet,然后在新的....上设置它。
Private datSet As DataSet
然后在你的新子......你可以设置它......
Public Sub New(ByVal dSet As DataSet)
datSet = dSet 'Now you have your DataSet and you can use it anywhere you want'
End Sub