[Answered my own question. See bottom of post.]
I have three datagridviews: Parent_Details, Child_Details, and Grandchild_Details. Each is bound to a datatable in a dataset, with data relations between them. I want to click on a record in parent and have the related records show up in child, and then child's related records in grandchild. Parent/child works well. How do i add granchild?
To test, i created a blank .net 5/winforms project with the following in form1.vb:
Public Class Form1
Private ReadOnly DataSet As New DataSet
Private ReadOnly Parent_Details As New DataGridView
Private ReadOnly Child_Details As New DataGridView
Private ReadOnly Grandchild_Details As New DataGridView
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Add_DataGridViews_To_Form()
Setup_DataSet()
Bind_Stuff()
End Sub
Private Sub Add_DataGridViews_To_Form()
Parent_Details.Size = New Size(255, 250)
Child_Details.Size = Parent_Details.Size
Grandchild_Details.Size = Parent_Details.Size
Parent_Details.Location = New Point(10, 10)
Child_Details.Location = New Point(Parent_Details.Location.X + Parent_Details.Size.Width + 10, Parent_Details.Location.Y)
Grandchild_Details.Location = New Point(Child_Details.Location.X + Child_Details.Size.Width + 10, Parent_Details.Location.Y)
Controls.Add(Parent_Details)
Controls.Add(Child_Details)
Controls.Add(Grandchild_Details)
End Sub
Private Sub Setup_DataSet()
DataSet.Tables.Add(New DataTable("Parent"))
With DataSet.Tables("Parent")
.Columns.Add("Id")
.Rows.Add(1)
.Rows.Add(2)
End With
DataSet.Tables.Add(New DataTable("Child"))
With DataSet.Tables("Child")
.Columns.Add("Id")
.Columns.Add("Parent")
.Rows.Add(1, 1)
.Rows.Add(2, 1)
.Rows.Add(3, 2)
.Rows.Add(4, 2)
End With
DataSet.Tables.Add(New DataTable("Grandchild"))
With DataSet.Tables("GrandChild")
.Columns.Add("Id")
.Columns.Add("Child")
.Rows.Add(1, 1)
.Rows.Add(2, 1)
.Rows.Add(3, 2)
.Rows.Add(4, 2)
.Rows.Add(5, 3)
.Rows.Add(6, 3)
.Rows.Add(7, 4)
.Rows.Add(8, 4)
End With
DataSet.Relations.Add(New DataRelation("Child_to_Parent", DataSet.Tables("Parent").Columns("Id"), DataSet.Tables("Child").Columns("Parent")))
DataSet.Relations.Add(New DataRelation("Grandchild_to_Child", DataSet.Tables("Child").Columns("Id"), DataSet.Tables("Grandchild").Columns("Child")))
End Sub
Private Sub Bind_Stuff()
Parent_Details.DataSource = New BindingSource With {.DataSource = DataSet, .DataMember = "Parent"}
Child_Details.DataSource = Parent_Details.DataSource
Child_Details.DataMember = "Child_to_Parent"
'Grandchild_Details.DataSource = Parent_Details.DataSource
'Grandchild_Details.DataMember = "Grandchild_to_Child"
End Sub
End Class
First datagridview is parent, the second is child, and the third is empty. Clicking on a record in parent changes the records shown in child. Great.
Now i want to show the grandchildren as well. Uncommenting the two commented out lines on bottom results in an error:
System.ArgumentException: 'DataMember property 'Grandchild_to_Child' cannot be found on the DataSource.'
How do i make this work for the grandchild as well?
Answered my own question:
Grandchild_Details.DataMember = "Child_to_Parent.Grandchild_to_Child"
My problem was i did not realize the DataMember pointed to the relation and not the table. I was using the table name in my code, and i only changed it in this very example for the purpose of illustration. Then it hit me, to try parent_relation.child_relation. Sounds backwords, but it works.