将DataRow绑定到另一个表中的另一个DataRow [英] Bind A DataRow to Another DataRow in a Different Table

查看:117
本文介绍了将DataRow绑定到另一个表中的另一个DataRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将控件绑定到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

推荐答案

当父行中的名称发生更改时,您不应该(根据您的要求)更新相关子行中的Name属性。您可以通过

GetParentRow
-dataview / adding-datarelations">
关系在两个表之间设置,并且来自父
GetChildRows
You should not have to (for what you are asking) update the Name property in the related child rows when the name changes in the parent row. You can get the name in the parent row from the child row via GetParentRow when a relationship is set between the two tables and from parent GetChildRows.


这篇关于将DataRow绑定到另一个表中的另一个DataRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆