I've posted this several times. It's a lot easier than
you think. Hope it helps.
Robin S.
------------------------------------
This may help. You might want to check out the BindingSource
component; using this for your databinding will more
easily enable the parent/child binding. Here's an example;
hopefully there will be something in here that you can use.
This example is for a form with two grids on it, and a bunch
of textboxes. One grid is the parent, the other is the
children, and the textboxes are bound to the parent.
---------------------------
I have a dataset that has two tables: Customers and Orders.
(This runs against Northwind, so if you have that
installed, you can try this out.)
I set up a strongly-typed dataset using the Data Set
Designer with both of those tables in it, and there is
a field called CustomerID in the Orders table that links
to the CustomerID table in Customers. So Orders have a
Customer as a parent. The data relation between
the customer and the orders is defined as FK_Orders_Customers.
(You can set up your dataset however you like.)
This is in Form_Load:
'First, populate the dataset.
Dim nwData As CustomersDataSet = CustomersDataSet.GetCustomers()
'Set the data source for the binding source to the dataset.
CustomersBindingSource.DataSource = nwData
'Add this so it knows which table to use .
CustomersBindingSource.DataMember = "Customers"
'Set the data source for the grid to the customers binding source.
CustomersDataGridView.DataSource = CustomersBindingSource
'Add the binding for the child; set its binding source to the same
' one as the parent.
Customers_OrdersBindingSource.DataSource = CustomersBindingSource
'Set the datamember to the foreign key joining the two tables.
' You can see this on your CustomersDataSet diagram.
Customers_OrdersBindingSource.DataMember = "FK_Orders_Customers"
'Set the data source on the child grid to points at its
' binding source.
OrdersDataGridView.DataSource = Customers_OrdersBindingSource
AddTextBoxDataBindings()
AddComboBoxDataBindings()
Here are the routines that bind the textboxes and combo box
in case you want them. These only bind to the Customers table,
but you can easily bind textboxes to the children by binding
to the Customers_OrdersBindingSource instead of the
CustomersBindingSource.
Private Sub AddTextBoxDataBindings()
'Bind each text box to the appropriate field in the
' CustomersBindingSource
CustomerIDTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CustomerID")
ContactNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "ContactName")
CompanyNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CompanyName")
ContactPhoneTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "Phone")
End Sub
Private Sub AddComboBoxDataBindings()
ContactsComboBox.DataSource = CustomersBindingSource
ContactsComboBox.DisplayMember = "ContactName"
ContactsComboBox.ValueMember = "CustomerID"
End Sub
This information came out of Brian Noyes's book on Data Binding.
---------------------------
Post by Keith ROk, I've been looking at all the properties (property window) and
reading the help file, and playing with different settings, but either
I'm missing something easy, or this isn't just a simple setting...
I have two datagridviews on my winform (vb.net 2005 express, SQL
express). They represent a parent and child table I have already
linked in DatabaseExplorer. I'd like the child table to only show the
child records associated with the parent record that is currently
selected.
Can anyone provide a web link or easy walkthrough? I'm perfectly
willing to read more in the help file, I just haven't found anything
yet that I can discern is what I need.
Thank you,
Keith