问题描述
您可以将控件绑定到DataSource(例如DataRow),但是您可以将数据行列绑定到另一个数据行列吗? 例如,我有一个类型化的数据集,其中包含两个不同的表Parent和Child。 我想将父行的Name属性
双向绑定到子行的ParentName属性。
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"})
我唯一的想法是subcr ibe到每个表的ColumnChanged事件并更新相关的数据行。 例如,如果父行的Name属性更新,我将更新相关子行的ParentName属性。
如果更新子行的ParentName,我将更新父行的名称。
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.
也许我可以更进一步,通过继承扩展父行和子行类,并添加一个DataBindings集合属性(就像控件一样)这将管理ColumnChanged的想法。
$
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
推荐答案
这篇关于将DataRow绑定到另一个表中的另一个DataRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!