Discussion:
Newbie needs help- parent and child datagrid views
(too old to reply)
Keith R
2006-12-10 00:01:39 UTC
Permalink
Ok, 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
Keith R
2006-12-10 00:10:58 UTC
Permalink
Arg, finally got it- I'll explain here, maybe someone can give me some
pointers, as maybe I'm doing something wrong.

In my datasources tab, I have my main dataset, and within that, my 3 tables
(one parent, one child, and one orphan). Within the Parent table in the
Datasources tab, there is a linked copy of my child table. Apparently, my
datagridview was pointing to the non-child copy of the child table (if that
makes sense). Should I just delete that table in my datasources tab, and
leave the one that is already linked as a child?

Thank you,
Keith
Post by Keith R
Ok, 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
RobinS
2006-12-10 05:38:36 UTC
Permalink
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 R
Ok, 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
Tejas patel
2010-04-16 13:30:10 UTC
Permalink
hi
pls help to one datagrid in add to two table one parent and child table

From http://www.developmentnow.com/g/40_2006_12_0_0_863403/Newbie-needs-help-parent-and-child-datagrid-views.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com/g/

Loading...