[C#winforms详细数据网格视图在添加新的父记录时不响应

问题描述 投票:0回答:1

使用Visual Studio 2019和SQL Server 12

我已经在winforms中实现了主从关系。父记录以表格形式显示,而该父行的子记录显示在datagridview中。只要查看现有记录,一切就可以正常进行。但是,当我添加新的父记录时,子datagridview仍然停留在查看的最后一个父(持久)记录上。新记录的临时ID为-1。我在数据库中使用了自动生成的ID。该数据库具有外键约束。当我将明细表添加到数据集时,关系出现了,但约束没有出现。我在有和没有约束的情况下进行了测试,但是行为没有变化。

我已经阅读了其他报告,说他们由于临时ID而无法保存详细记录。对我来说,当我添加新的子行时,它链接到其他父记录,而不是新的父记录。

c# winforms datagridview
1个回答
0
投票

您的bindingNavigator应该绑定到您的ParentBindingSource,并且您的childBindingSource应该绑定到ParentBindingSource的子成员。

假设您有两个表:Customers(父项)为customersBindingSource,Contacts(子级)为contactsBindingSource。因此,联系人具有链接到的客户的外键。

因此contactsBindingSource应该链接到customersBindingSource。类似于:

this.contactsBindingSource.DataSource = this.customersBindingSource;
this.contactsBindingSource.DataMember = "CustomersContacts";

从那里,所有孩子(联系人)都与父母(客户)链接。

希望有所帮助,因为我们真的不知道您是如何实现这种关系的。正如Jeroen van Langen所说的那样,共享您的代码的一部分来显示如何实现这种关系将是不胜感激的。

© www.soinside.com 2019 - 2024. All rights reserved.