简体   繁体   English

如何从父文本框中获取FK以在子datagrid中使用

[英]How to get the FK from a parent text box to use in a child datagrid

I have two tables tblParent and tblChild with a FK in tblChild. 我在tblChild中有一个FK有两个表tblParent和tblChild。 I put these two tables into a dataset and set the relationship to cascade I can put both onto a form and add a parent and child records and then save and all works as it should. 我将这两个表放入数据集中,并将关系设置为级联。我可以将两者都放在表单上,​​并添加父记录和子记录,然后保存并按预期工作。

But

I want to use text boxes for the parent not a datagrid. 我想为父级而不是数据网格使用文本框。 When i DnD the fields and then add a child datagrid the child no longer automatically updates when i save, i get a missing FK exception 当我确定字段并添加子数据网格时,在保存时该子项不再自动更新,我得到了丢失的FK异常

Please advise what i need ot do to get the same function as i have with two datagrids. 请告知我需要做些什么才能获得与两个数据网格相同的功能。

thanks 谢谢

John 约翰

I've found one way of doing this, it may not be the right way but it does work 我找到了一种方法,可能不是正确的方法,但确实有效

In the form load event I have disabled the relationship constraints: 在表单加载事件中,我禁用了关系约束:

    Me.Dataset.EnforceConstraints = False

then when i come to save: 然后,当我来保存:

    Me.TblParentBindingSource.EndEdit()
    Me.TblChildBindingSource.EndEdit()

    Me.TableAdapterManager.UpdateAll(Me.Dataset)
    Me.TblChildTableAdapter.Update(Me.Dataset.tblChild)

after the tableadapter.updateall you can see the fk values propogate in the child datagrid and you can see the childIDs as negatives hence the final update on the child table 在tableadapter.updateall之后,您可以在子数据网格中看到fk值传播,并且可以看到childID为负数,因此对子表进行了最终更新

I hope this is helpful to someone. 我希望这对某人有帮助。 If someone can give me a better approach I'd be grateful. 如果有人可以给我更好的方法,我将不胜感激。

John 约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM