【问题标题】:C# winforms detail datagridview not responding when a new parent record is addedC# winforms detail datagridview 在添加新父记录时没有响应
【发布时间】:2020-04-29 13:52:36
【问题描述】:

使用 Visual Studio 2019 和 sql server 12

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

我读过其他人报告说他们因为临时 ID 而无法保存详细记录。对我来说,当我添加新的子行时,它会链接到不同的父记录,而不是新的父记录。

ResearchActivity 和联系人之间存在多对多关系。涉及的三个表是 ResearchActivities、Contacts 和 ResearchActivitiesContacts。我在这里将 ResearchActivity 视为父母,将 ResearchActivityContacts 视为孩子。 分享一些代码: 关系 fkc = new global::System.Data.ForeignKeyConstraint("FK__ResearchActvityContacts__ResearchActivity", new global::System.Data.DataColumn[] { this.tableResearchActivity.ResearchActivityIDColumn}, new global::System.Data.DataColumn[] { this.tableResearchActivityContacts.ResearchActivityIDColumn}); this.tableResearchActivityContacts.Constraints.Add(fkc); fkc.AcceptRejectRule = global::System.Data.AcceptRejectRule.None; fkc.DeleteRule = global::System.Data.Rule.None; fkc.UpdateRule = global::System.Data.Rule.Cascade;

孩子的绑定源 this.researchActivityContactsBindingSource.DataMember = "FK__ResearchActvityContacts__ResearchActivity"; this.researchActivityContactsBindingSource.DataSource = this.researchActivityBindingSource;

        this.researchActivityBindingNavigator.BindingSource = this.researchActivityBindingSource;
        this.researchActivityContactsBindingNavigator1.BindingSource = this.researchActivityContactsBindingSource;

【问题讨论】:

  • 没有任何代码,很难判断哪里出错了。
  • @user994124:你试过dataGridViewName.Refresh();吗??
  • @D J。感谢您的建议。尝试在添加新按钮的单击事件中刷新。也许,我比我应该更快地调用刷新。您对我可以调用刷新的任何其他事件有什么建议吗?
  • @Jeroen van Langen 不确定您希望我分享代码的哪些部分
  • @user994124:你是如何实现主从关系的?使用 BindingSource 组件?你如何添加记录?手动?通过使用 bindingsource.AddNew() 方法?

标签: c# winforms datagridview


【解决方案1】:

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

假设您有两个表:作为customersBindingSource 的客户(父)和作为contactsBindingSource 的联系人(子)。所以联系人有一个他们链接到的客户的外键。

所以contactsBindingSource 应该链接到customersBindingSource。类似的东西:

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

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

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

【讨论】:

  • 添加了一些代码 sn-ps。只要我正在查看数据库中的数据,一切都会正常工作。问题仅在我创建新的父记录时(在我的情况下为 ResearchActivity)。此外,一旦我保存了新记录,我就可以添加子记录。在创建和保存之间的时间里,子 datagridview(s) 显示杂散数据(实际上是上次查看的持久父记录的子记录)。
  • 试试这个,让我们知道结果:将 PositionChanged 事件订阅到 researchActivitiesBindingSource (researchActivitiesBindingSource.PositionChanged += this.ResearchActivitiesBindingSource_PositionChanged;)。在生成的方法中,添加对子数据源的引用并对其进行中断(类似于 Console.WriteLine(this.researchActivitiesContactsBindingSource);)。查看 bindingsource 的属性并告诉我们添加新记录时 Count 和 Current 属性中有哪些值(应为 Current = null 和 Count = 0)
  • 谢谢。奇怪的事情发生了。添加了事件。 current 为 null 且 count 为 0,但现在 datagridview 已清除。它是空白的。但是,我现在有两个问题。首先,我只能向子数据网格视图添加一行。当我尝试添加第二个时,它抱怨约束并说父级必须存在。当我将第一行添加到子 dgv 时,它正确地将父(临时)ID 显示为-1。但是,当我保存记录时,只存储父级。我在 FK__ResearchActvityContacts__ResearchActivity 中有约束和关系
  • 您的 researchActivitiesContactsBindingSource 的 AllowNew 设置为 True 吗?你能分享你用来将数据保存回数据库的方法的代码吗?假设您正在使用 DataSet,您应该调用 tableAdapterManager.UpdateAll 来保存子记录。
  • private void researchActivityBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); moveFromCheckedListBoxItems(); this.researchActivityBindingSource.EndEdit(); this.researchActivityTeamMembersBindingSource.EndEdit(); this.researchActivityContactsBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.cRM2DataSet); }
猜你喜欢
  • 2022-01-04
  • 2018-07-10
  • 2014-07-14
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
相关资源
最近更新 更多