Pass Parent Primary Key To New Rows In Master Details

When I create a new row in a Master Details page, I wish to be able to pass along the primary key of the parent
record to it so that I can set the child's foreign key to that, which is represented by the ParentShift property in the child table in my model.

Ideally when the row has been validated I should be able to retrieve the Parent key, set the child's foreign key as that and upload the whole thing to the database.

My relational column on the Master Details is set to the property SubShifts which is a list of the SubShifts in the parent table (Shift).  AutoGenerateRelations is False and AutoGenerateColumns is False.


My models are as follows:

Shift (Parent)
int Id 

string? Name
Worker Worker

List<SubShift>? SubShifts

SubShift (Child)

int Id

Client Client

Shift ParentShift

 This is how my Data Grid looks currently:


mster detail example.png


I believe my question is similar to this one which however is for ASP:

https://www.syncfusion.com/forums/125157/add-parent-record-primary-key-values-to-new-rows-in-a-child-grid


3 Replies 1 reply marked as answer

SB Sweatha Bharathi Syncfusion Team June 18, 2025 12:55 PM UTC

Hi Alfred Smith ,

We have reviewed your query. Based on the details and the image you provided, we suspect that you are using AddNewRowPosition. We have prepared a sample based on the information you shared. When creating a new row, it is possible to retrieve the parent key. Setting the child's foreign key can be achieved in two ways:

1.When the row has been validated.

2.When the new row has been initiated.

Solution 1: When the row has been validated.

When the row has been validated, the RowValidating event will be triggered. In this event, you can access the DetailsViewDataGrid of the currently validating row. From there, you can retrieve the parent data grid using the GetParentDataGrid method. Once you have the parent data grid, you can get the CurrentItem of the parent row data.

Then, by accessing the CurrentDataRow of the DetailsViewDataGrid via e.RowData, you can set the CurrentItem into the ParentShift property of the SubShift (child grid).

Code snippet to retrieve the primary parent key in RowValidating event:

 private void OnRowValidated(object sender, Syncfusion.UI.Xaml.DataGrid.RowValidatedEventArgs e)

 {

     if (e.OriginalSender is DetailsViewDataGrid detailsGrid)

     {

         var parentDataGrid = detailsGrid.NotifyListener?.GetParentDataGrid();

         if (parentDataGrid == null)

             return;

         var currentItem = parentDataGrid.CurrentItem;

         if(currentItem != null && currentItem is Shift)

         {      

            var currentDataRow = parentDataGrid.CurrentItem as Shift;

             if(e.RowData != null && e.RowData is SubShift)

             {

                 var subShift = e.RowData as SubShift;

                 subShift.ParentShift = currentDataRow;  // Set the foreign key to refer to the parent

             }

         }            

     }

 }


Solution 2: When the new row has been initiated.

When a new row is being initiated, the AddNewRowInitiating event will be triggered. In this event, you can access the DetailsViewDataGrid of the currently initiating row. From there, you can retrieve the parent data grid using the GetParentDataGrid method. Once you have the parent data grid, you can get the CurrentItem of the parent row data.

Then, by accessing the CurrentDataRow of the DetailsViewDataGrid via e.NewObject, you can assign the CurrentItem to the ParentShift property of the SubShift (child grid).

This approach saves the value of the ParentShift property during initialization itself, before committing the row.

Code snippet to retrieve the primary parent key in AddNewRowInitiating event:

 private void OnAddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs e)

 {

     if (e.OriginalSender is DetailsViewDataGrid detailsGrid)

     {

         var parentDataGrid = detailsGrid.NotifyListener?.GetParentDataGrid();

         if (parentDataGrid == null)

             return;

         var currentItem = parentDataGrid.CurrentItem;

         if (currentItem != null && currentItem is Shift)

         {

             var currentDataRow = parentDataGrid.CurrentItem as Shift;

             if (e.NewObject != null && e.NewObject is SubShift)

             {

                 var subShift = e.NewObject as SubShift;

                 subShift.ParentShift = currentDataRow;

             }

         }

     }

 }


Find the sample in the attachment and let us know if you have any concerns on this.

Note : Based on the details you provided, the ParentShift property is of type Shift, so we assign the parent data row directly to the ParentShift property. If you need to assign a specific property from the parent, you will need to extract that property from the parent data row accordingly.


Regards,

Sweatha B


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDataGridDemoSample_af3367b4.zip

Marked as answer

AS Alfred Smith June 22, 2025 02:25 PM UTC

Thanks very much  Sweatha that solved my problem :)



SB Sweatha Bharathi Syncfusion Team June 23, 2025 05:10 AM UTC

Hi Alfred Smith ,

Glad that your issue is resolved!!. Please get back to us if you need any further assistance, we will be happy to help you.


Regards,
Sweatha B

Loader.
Up arrow icon