Question Storing data from a .mdf to my class

roary86

Member
Joined
Mar 16, 2009
Messages
12
Programming Experience
1-3
Overview:
This is VS 08 windows application using a .mdf database that was created in VS and is inside the project itself.
2 forms, first form has a dropdown list that is populated from my ClientName column in my Client table table inside my clients.mdf database that I created in VS.

2nd form has text fields that all the data that is associated with the dropdown list's selected item.

So if you choose ex1 from form1 then form2 will have all of ex1's remainging data.

The problem, I'm trying to take the selected item along with the associated data and store it into my ClientAccount Class but I can not figure out how to do that. I know I should populate the dropdown in the form load but I need to be doing the rest inside the selected index change on the drop down.

Form1 Form Load
VB.NET:
            'Set up and filld the DataSet
            AClientDataSet = New ClientDataSet
            AClientsTableAdapter = New ClientDataSetTableAdapters.ClientTableAdapter
            AClientsTableAdapter.Fill(AClientDataSet.Client)

            Try
                'Set up the binding source.
                AClientsBindingSource = New BindingSource
                With AClientsBindingSource
                    .DataSource = Me.AClientDataSet
                    .DataMember = "Client"
                    ' Get the correct count of the rows in the DataSet.
                    .MoveLast()
                    .MoveFirst()
                End With

                'Bind the controls
                With Me.cboClient
                    .DataSource = AClientsBindingSource
                    .DisplayMember = "ClientName"
                    .DataBindings.Add("text", Me.AClientsBindingSource, "ClientName", False, DataSourceUpdateMode.Never)
                End With

            Catch ex As Exception
                MessageBox.Show("Data Error: " & ex.Message)
            End Try
 
Your binding is set up manually? I'd have just dragged the combo out of the Data Sources window and onto the form. It would automatically declare a form-level bindingsource for you.

Note, you have to expand the Data Sources tree and change the relkevant detail from a text box to a combo before you drag it. If this puzzles you, expand every node in the Data Sources window and look at them. All the leaf nodes are dropdowns; change them and have a play


Next, when you make your Form2, have it have a constructor that takes a BindingSource as a parameter. THen simply pass the Form1 bindingsource to the form2. Every control on Form2 will be bound to (another) bindingsource if you have drag/dropped out of Data Sources, but you can simply make FOrm2's bindingsource.DataSOurce = Form1's binding source

Let us assume your datatable is called MyWhatever. You have dragged controls from Data SOurces onto both form1 and form2. VS has created two bindingsources, both called MyWhateverBindingSOurce, one on form1, one on form2
VB.NET:
'in form1

Dim f as New Form2(Me.MyWhateverBindingSource)



'in form 2
Public Sub New (bs as BindingSource)
  Me.MyWhateverBIndingSource.DataSOurce = bs
End Sub
Now changes to the position on form1 are reflected on form2
 

Latest posts

Back
Top