0
votes

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;
1
It's very hard to tell where it went wrong, without any code.Jeroen van Langen
@user994124: Did you try dataGridViewName.Refresh(); ??D J
@D J. Thanks for the suggestion. Tried the refresh in the click event of the add new button. Maybe, I am calling the refresh sooner than I should. Do you have suggestions for any other event where I can call the refresh?user994124
@Jeroen van Langen Not sure what parts of the code you want me to shareuser994124
@user994124 : How are you implementing the master detail relationship ? With the BindingSource Component ? How do you add the record ? manually ? by using the bindingsource.AddNew() method ?JSL

1 Answers

0
votes

Your bindingNavigator should be bound to your ParentBindingSource, and your childBindingSource to the child member of your ParentBindingSource.

Assume you have two tables : Customers (Parent) as customersBindingSource and Contacts (Child) as contactsBindingSource. So Contacts have a foreign key of the customer they are linked to.

So contactsBindingSource should be linked to customersBindingSource. Something like :

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

From there, all child (contacts) are linked to parents (customers).

Hope that helps, as we really don't know how you implemented this relation. As Jeroen van Langen said, sharing parts of your code showing how you implement this relation would be appreciated.