CRUD for foreign key column

Suppose you have this sample's OrderDetails.cs:

https://blazor.syncfusion.com/documentation/datagrid/foreignkey-column?cs-save-lang=1&cs-lang=razor#binding-local-data

and then the following Index.razor code, where Args.Data can be used for performing CRUD in the Orders table. The problem is: how to use Args.Data to know which row of the Employees table to delete, update, insert, if the employee name column in the grid is edited, and assume one employee at most handles one order?


@using Syncfusion.Blazor.Grids


<SfGrid DataSource="@Orders" Height="315">

    <GridEvents OnActionBegin="ActionBegin" TValue="GanttSegClass"></GridEvents>

    <GridColumns>

        <GridColumn Field=@nameof(OrderDetails.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>

        <GridForeignColumn Field=@nameof(OrderDetails.EmployeeID) HeaderText="Employee Name" ForeignKeyValue="FirstName" ForeignDataSource="@Employees" Width="150"></GridForeignColumn>

        <GridColumn Field=@nameof(OrderDetails.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>

        <GridColumn Field=@nameof(OrderDetails.ShipCity) HeaderText="Ship City" TextAlign="TextAlign.Right" Width="120"></GridColumn>

    </GridColumns>

</SfGrid>

@code {

    public List<OrderDetails> Orders { get; set; }

    public List<EmployeeDetails> Employees { get; set; }

    protected override void OnInitialized()

    {

        Orders = OrderDetails.GetAllRecords();

        Employees = EmployeeDetails.GetAllRecords();

    }

    public void ActionBegin(Syncfusion.Blazor.Grids.ActionEventArgs<OrderDetails> Args)

    {

        if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save)){

            if (Args.Action == "Add"){

                //add Args.Data

            }

            else{

                //update Args.Data

            }

        }

        if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete)){

            //delete Args.Data

        }

    }

}


1 Reply

NP Naveen Palanivel Syncfusion Team February 19, 2025 01:12 PM UTC

Hi desmond,

Greeting form the Syncfusion,

We have reviewed your query and would like to clarify that when performing CRUD operations in the Grid, the changes are applied only to the Orders table. The Employees table remains unaffected. In this case, the Grid maps the EmployeeID from the Orders table to the corresponding EmployeeName from the Employees table using the foreign key column. This is the default behavior of the Grid. Therefore, when editing the EmployeeName column, Args.Data will only provide the EmployeeID because the changes are being made to the Orders table, not the Employees table.

Regards,
Naveen.


Loader.
Up arrow icon