Winforms .net 2.0:将文本框绑定到父属性
我有一个强类型的数据集,带有两个数据表:父级和子级,带有以您期望的方式链接它们的关系(例如父级有很多子级)。
在我的“孩子详细信息”表单上,我显示了有关当前孩子的大量信息,带有如下所示的绑定调用:
me.txtBirthDate.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Child.Birthdate"))
但我还想显示有关孩子父母的一些信息 - 例如父母的姓名。 我已经尝试过:
me.txtParentName.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Child.Parent.Name"))
和
me.txtParentName.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Parent.Name"))
但这都会导致空白文本框。
我当然可以将父属性直接放在子数据表上 用底层数据库表之间的联接结果填充它们,但如果可能的话,我想避免这种情况(我真正的应用程序只涉及几个父母,每个父母都有很多孩子,我不想成为移动了很多不必要的数据)。
那可能吗?
非常感谢!
-罗伊
I've got a strongly typed dset, w/two datatables: Parent and Child, w/a relation linking them in the way you'd expect (e.g. parent-has-many-children).
On my Child Detail form, I show lots of info on the current child, w/binding calls like so:
me.txtBirthDate.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Child.Birthdate"))
But I would also like to show some info on the child's parent--say, the parent's name. I have tried:
me.txtParentName.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Child.Parent.Name"))
and
me.txtParentName.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Parent.Name"))
But these both result in a blank text box.
I can of course put the parent properties directly on the Child DataTable & fill them w/the results of a join between the underlying db tables, but I'd like to avoid that if it's possible (my real app involves just a few Parents each w/many many Children & I'd like not to be moving so much unnecessary data).
Is that possible?
Many thanks!
-Roy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常在
DataSet
和控件之间使用BindingSource
。 使用此方法时,如果我想要将控件绑定到不同表中的相关行,我会创建一个新的BindingSource
,它指向相关行(他的DataSource
code> 属性是原来的BindingSource
,他的DataMember
是父表),然后将其他控件绑定到第二个BindingSource
。我通常在设计器视图中完成这一切,它只是在我进行过程中创建那些
BindingSource
对象。希望有帮助。
I usually use a
BindingSource
between theDataSet
and the controls. When using this approach, if I have controls that I want to bind to a related row from a different table, I create a newBindingSource
, which points at the related row (hisDataSource
property is the originalBindingSource
, hisDataMember
is the parent table), and then bind the other controls to the 2ndBindingSource
.I usually do it all in the designer view, which just creates those
BindingSource
objects as I go along.Hope that helps.