Using visual studio 2019 and sql server 12
I have implemented a master detail relationship in winforms. The parent record is shows in a form and the child records for that parent row are shown in a datagridview. Things work fine as long I view the existing records. However, when I add a new parent record, the child datagridview is still stuck at the last parent (persisted) record viewed. The new record has a temp id of -1. I use an autogenerated id in the db. The db has a foreign key constraint. When I added the detail table to the dataset, the relationship appeared but the constraint did not. I tested with and without the constraint, but there is no change in behavior.
I have read others reporting that they were not able to save the detail records because of the temp id. For me when I add a new child row, it is linked to a different parent record, not the new parent record.
There is a many to many relationship between ResearchActivities and Contacts. The three tables involved are ResearchActivities, Contacts and ResearchActivitiesContacts. I am treating ResearchActivity as the parent here and ResearchActivitiesContacts as the child. Sharing some code: relationship 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;
binding source for child this.researchActivityContactsBindingSource.DataMember = "FK__ResearchActvityContacts__ResearchActivity"; this.researchActivityContactsBindingSource.DataSource = this.researchActivityBindingSource;
this.researchActivityBindingNavigator.BindingSource = this.researchActivityBindingSource;
this.researchActivityContactsBindingNavigator1.BindingSource = this.researchActivityContactsBindingSource;
dataGridViewName.Refresh();
?? – D J