Bind A DataRow to Another DataRow in a Different Table

  • Thread starter Thread starter Ryan0827
  • Start date Start date
R

Ryan0827

Guest
You can bind a control to a DataSource such as a DataRow, but can you bind a datarow column to another datarow column? For example, I have a typed dataset that contains two different tables Parent and Child. I want to 2-way bind the Name property of the parent row to the ParentName property of the child row.

Dim parent As New DataTable("Parent")
parent.Columns.Add("ParentID", GetType(Integer))
parent.Columns.Add("Name", GetType(String))

Dim child As New DataTable("Child")
child.Columns.Add("ChildID", GetType(Integer))
child.Columns.Add("ParentID", GetType(Integer))
child.Columns.Add("ParentName", GetType(String))

parent.Rows.Add({1, "John"})
parent.Rows.Add({2, "Sam"})
child.Rows.Add({1, 1, "John"})
child.Rows.Add({2, 1, "John"})
child.Rows.Add({3, 2, "Sam"})


The only idea I have is to subscribe to the ColumnChanged event of each table and update the related datarow(s). For example, if the Name property of a parent row is updated, I'd update the ParentName property of the related child rows. If the ParentName of a child row is updated, I'd update Name of the parent row.


Perhaps I could take it a step further and extend the parent and child row classes by inheritance and add a DataBindings collection property (just like controls) that would manage the ColumnChanged idea.


Ryan

Continue reading...
 
Back
Top