How to define the relationship to parent when adding a new row in master-details configuration

I need to get reference to the item (object) in the parent row of the master detail relationship so I can set the relationship in the new item (object) when a new record is created.  The AddNewRowInitiating event does not contain any information related to the parent.

Please advise

5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team November 9, 2020 01:45 PM UTC

Hi Robert Angus,

Thank you for contacting Syncfusion support.

Your requirement can be achieved by using underlying data object must be defined with default constructor. Otherwise, create instance of data object by handling AddNewRowInitiating event. Please refer the below code snippet, 

XAML Code snippet:

 
<syncfusion:SfDataGrid x:Name="dataGrid" 
                                       AutoGenerateColumns="False" 
                                       AutoGenerateRelations="False" 
                                       AddNewRowPosition="Top" 
                                       ItemsSource="{Binding Path=OrdersDetails}" 
                                       NavigationMode="Cell" 
                                       ShowGroupDropArea="True"> 
            <syncfusion:SfDataGrid.DetailsViewDefinition> 
                <syncfusion:GridViewDefinition RelationalColumn="OrderDetails"> 
                    <syncfusion:GridViewDefinition.DataGrid> 
                        <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid" AddNewRowPosition="Top" 
                                        AutoGenerateColumns="True"> 
                            <syncfusion:SfDataGrid.Columns> 
                                <syncfusion:GridTextColumn MappingName="OrderID" /> 
                                <syncfusion:GridTextColumn MappingName="ProductID" TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn HeaderText="Unit Price" 
                                                                   MappingName="UnitPrice" 
                                                                   TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn MappingName="Quantity" TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn MappingName="Discount" TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn MappingName="CustomerID" /> 
                                <syncfusion:GridDateTimeColumn HeaderText="Order Date" 
                                                                       MappingName="OrderDate" 
                                                                       TextAlignment="Right" /> 
                            </syncfusion:SfDataGrid.Columns> 
                        </syncfusion:SfDataGrid> 
                    </syncfusion:GridViewDefinition.DataGrid> 
                </syncfusion:GridViewDefinition> 
            </syncfusion:SfDataGrid.DetailsViewDefinition> 
            <syncfusion:SfDataGrid.Columns> 
                <syncfusion:GridTextColumn MappingName="OrderID" /> 
                <syncfusion:GridTextColumn MappingName="CustomerID" /> 
                <syncfusion:GridDateTimeColumn Width="200" 
                                                       HeaderText="Shipping Date" 
                                                       MappingName="ShippingDate" 
                                                       TextAlignment="Right" /> 
                <syncfusion:GridNumericColumn HeaderText="Supplier ID" 
                                                      MappingName="EmployeeID" 
                                                      NumberDecimalDigits="0" /> 
                <syncfusion:GridTextColumn HeaderText="Ship City" MappingName="ShipCity" /> 
                <syncfusion:GridTextColumn HeaderText="Ship Country" MappingName="ShipCountry" /> 
                <syncfusion:GridCurrencyColumn CurrencySymbol="$" MappingName="Freight" /> 
                <syncfusion:GridCheckBoxColumn HeaderText="Closed" MappingName="IsClosed" /> 
            </syncfusion:SfDataGrid.Columns> 
</syncfusion:SfDataGrid> 


C# Code snippet:

 
dataGrid.AddNewRowInitiating += DataGrid_AddNewRowInitiating;

private void DataGrid_AddNewRowInitiating(object sender, Syncfusion.UI.Xaml.Grid.AddNewRowInitiatingEventArgs e) 
            (e.NewObject as OrderInfo).OrderDetails = new ObservableCollection<OrderDetails>(); 
} 
 

We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S




Marked as answer

RA Robert Angus November 9, 2020 02:12 PM UTC

Thanks for the reply.  This does not solve my issue.  I want to get a reference to the parent (master) object.  I am trying to add a new detail record.  The AddNewRowInitiating event does not give me a reference to the parent item (object).  I can get a reference to the detail grid, from which I can call GetParentDataGrid then I can request the use the selectedItem property, but is it guaranteed that this will always have the correct selection?



VS Vijayarasan Sivanandham Syncfusion Team November 10, 2020 05:44 PM UTC

Hi Robert Angus,

Thanks for the update.

Based on provided information you can get the parent item by using SelecteItem property in GetParentDataGrid and also you can get the Parent item by using CurrentItem property in GetParentDataGrid method in SfDataGrid. Please refer the below sample and user documentation for more reference,

 
private void FirstDetailsViewGrid_AddNewRowInitiating(object sender, Syncfusion.UI.Xaml.Grid.AddNewRowInitiatingEventArgs e) 
            var currentItem = (e.OriginalSender as SfDataGrid).GetParentDataGrid().CurrentItem; 
 
            var selectedItem = (e.OriginalSender as SfDataGrid).GetParentDataGrid().SelectedItem; 

Sample Link: https://www.syncfusion.com/downloads/support/forum/159515/ze/ModifiedSample639853791

We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S





TI Tim replied to Vijayarasan Sivanandham October 27, 2022 10:06 PM UTC

Vijay, this property is misleading. When multiple rows have been expanded then the parent data grid currentitem and selected item always return the last clicked parent row. Which is incorrect.

Example below:

Master Grid 
--Parent 1 (last parent row clicked)
----- Detail 1 expanded
--Parent 2 
---- Detail 2 expanded


When you ask detail 2 grid what his parents current item is in this case you will get Parent 1. Which is incorrect.



VS Vijayarasan Sivanandham Syncfusion Team October 28, 2022 03:43 PM UTC

Hi Tim,

We have checked the reported issue “Incorrect parent row when there are multiple subgrids expanded” and are unable to replicate the issue from our end. Please find the tested sample and video demo in the attachment.

Please have a look at this sample and let us know if we have missed any customization done in your application. Otherwise, try to reproduce the reported issue in this sample and revert to us with the modified sample and steps to replicate the issue. It will be more helpful for us to find the exact cause of the issue and to provide a prompt solution.

Regards,
Vijayarasan S

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



Attachment: Sample_And_Video_Demo_ad030408.zip

Loader.
Up arrow icon