How do I bind a TextBox to a nested relation

Platform: WinForms| Category: Data Binding

Say you have a Parent table related to a Child table related to a GrandChildTable, and you want to bind a TextBox to a column in the GrandChild table.

To do this you have to have nested relations defined, and you use this nesting to create the DataMember parameter for the the DataBinding that you add to the TextBox. Below are some code snippets. They show the DataMember as ”ParentToChild.ChildToGrandChild.Name” which creates a path from the parent table down to the field in the nested relation by stringing the relation names together separated with a period. You can also download both C# and VB projects.


       Dim dSet As New DataSet()

        ’get the tables
        Dim parentTable As DataTable = GetParentTable()
        Dim childTable As DataTable = GetChildTable()
        Dim grandChildTable As DataTable = GetGrandChildTable()
        dSet.Tables.AddRange(New DataTable() {parentTable, childTable, grandChildTable})

        ’setup the relations
        Dim parentColumn As DataColumn = parentTable.Columns(''parentID'')
        Dim childColumn As DataColumn = childTable.Columns(''ParentID'')
        dSet.Relations.Add(''ParentToChild'', parentColumn, childColumn)

        parentColumn = childTable.Columns(''childID'')
        childColumn = grandChildTable.Columns(''ChildID'')
        dSet.Relations.Add(''ChildToGrandChild'', parentColumn, childColumn)

        Me.TextBox1.DataBindings.Add(''Text'', parentTable, ''ParentToChild.ChildToGrandChild.Name'')

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.