Question

I am loading personnel data from database and fill a dataset as follows:

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")

Now when I double click any personnel in the dgvPersonnel I load an other form "frmPersonnelDatails" to be able to modify personnel data where I also have a combobox to select the department of the personnel.

My Concern now is how can I set the datasource of the combobox in the "frmPersonnelDatails" form to be the department table in my dataset instead of querying the database again to load then. Meaning I do not want to query the data more than one time. I tried the following but could not get the results:

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
Was it helpful?

Solution

In order to set the datasource of the combobox in your other form send this DataSet in your constructor...

Here's an example:

 Dim fPD As frmPersonnelDatails = New frmPersonnelDatails('YOUR DS') 'Pass your dataset in the constructor'
 fPD.ShowDialog()

In your class: frmPersonnelDatails make sure there's a new constructor...

 Sub New(ByVal dSet As DataSet)
    cmbDepartment.DataSource = dSet.Tables("department")
    cmbDepartment.DisplayMember = "name"
    cmbDepartment.ValueMember = "depId"
 End Sub

Another option is to declare a new DataSet in your class as so and then set this on the new....

 Private datSet As DataSet

Then in your new sub.... you can set it...

 Public Sub New(ByVal dSet As DataSet)
    datSet = dSet 'Now you have your DataSet and you can use it anywhere you want'
 End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top