Clone of list is edited

Dear Syncfusion,


I want something relative easy.

at my void OnInitialized() method I am getting data from the database.

In my class there is another list (List<class>) That is also getting data from the database.

I have a hierarchy-grid where my second datasource is the list within my first datasource.

I create a clone when I am in the OnInitialized() so I have a list that is "original".

In the second part of the grid people are allowed to change almost everything, but when the changes happen and they are ready the press send (calls a method that should look into both the edited and the original list) the original list is also changed. How can I have an original list without any changes?

Example:

<SfGrid DataSource="@datasource">

        <GridTemplates>

            <DetailTemplate>

                @{

                     newContext = (context as Employee);

                    <SfGrid @ref="@OrderGrid" TValue="Orders" DataSource="employee.Orders" AllowPaging="true">

                        <GridPageSettings PageSize="8"></GridPageSettings>

                        <GridEditSettings AllowEditing="true" Mode="EditMode.Batch"></GridEditSettings>

                        <GridColumns>

 

<GridColumn Field=@nameof(Order.Example1) TextAlign="TextAlign.Center" HeaderText="example" IsPrimaryKey="true" AllowEditing="false" EditType="EditType.NumericEdit"></GridColumn>

<GridColumn Field=@nameof(Order.Example2) TextAlign="TextAlign.Center" HeaderText="example2" AllowEditing="true" EditType="EditType.NumericEdit"></GridColumn>

<GridColumn Field=@nameof(Order.Example3) TextAlign="TextAlign.Center" HeaderText="example3" AllowEditing="true" EditType="EditType.NumericEdit"></GridColumn>

<GridColumn Field=@nameof(Order.Example4) TextAlign="TextAlign.Center" HeaderText="example4" AllowEditing="true" EditType="EditType.Default"></GridColumn>

                        </GridColumns>

                    </SfGrid>

                }

            </DetailTemplate>

        </GridTemplates>

        <GridColumns>

            <GridColumn Field=@nameof(Employee.ID) HeaderText="ID" IsPrimaryKey="true" TextAlign="TextAlign.Left"> </GridColumn>

            <GridColumn Field=@nameof( Employee .Name) HeaderText=" Name "></GridColumn>

            <GridColumn Field=@nameof( Employee .Adress) HeaderText=" Adress "></GridColumn>

            <GridColumn Field=@nameof( Employee .Status) HeaderText="Status"></GridColumn>

        </GridColumns>

        <SfButton @onclick="@((args) => Save(args,null))" IsPrimary="true">Verzenden</SfButton>

    </SfGrid>



 protected override void OnInitialized()

    {

Employees= _db.GetData<Employee>();

 for (int i = 0; i < Employees .Count; i++)

        {

             Employees [i].Orders= _db.GetLinkedData<Orders>();

        }


        datasource = new List<Employee>( Employees); //Clone

}


public async Task Save(Microsoft.AspNetCore.Components.Web.MouseEventArgs args, null)

    {

        await grid.EndEdit();//Save the edited/added data to Grid

         var original = Employees[0].Orders[2];

     var changed = Datasource[0].Orders[2];

    }



But still original and changed both have the same value when the value is changed in the grid.
What is causing this and how to do it so I can have an edited and an orignal list.


3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team August 9, 2021 02:11 PM UTC

Hi Bossink, 

Greetings from Syncfusion support. 

We checked this scenario by creating a sample based on your shared codes. But we could get proper original data values gathered in OnInitialized in the Save method also. We are attaching the sample which we have tried for your reference. Please download the sample from the link below, 
 
And also, to fetch the batch changes, we suggest you to use the GetBatchChangesAsync method of Grid. This method call returns the added/deleted/edited changes made in grid. Please refer the code below, 

 
public async Task Save(Microsoft.AspNetCore.Components.Web.MouseEventArgs args) 
{ 
    await OrderGrid.EndEdit();//Save the edited/added data to Grid 
    var original = Employees[0].Orders[2]; 
     //var changed = Datasource[0].Orders[2]; 
    var changed = await OrderGrid.GetBatchChangesAsync(); 
} 


Kindly refer the above attached sample and suggestion. If you are still facing any difficulties then the following details would be helpful for us to proceed further. 

  1. Share a detailed explanation of the problem you are facing.
  2. Share the sample which you have tried from your side.
  3. Or if possible, reproduce the problem with the above attached sample and share with us for further analysis.
  4. Share the video demo showing the replication of the problem you are facing.

The provided information will help us analyze the problem, and provide you a solution as early as possible. 

Regards, 
Renjith R 



BO bossink August 16, 2021 10:21 AM UTC

Dear Syncfusion,


Thank you for the reply.

I added the file with the next step.

I created a new instance and I want to keep the original and I want to add the changes to the original in a new instance.


Regards,


Bossink


Attachment: ServerApp_81420a0f.zip


RS Renjith Singh Rajendran Syncfusion Team August 17, 2021 12:08 PM UTC

Hi Bossink, 

Based on your scenario, we suggest you to refer the below general documentation links for cloning, without affecting the original. 

We have also modified the shared sample for your convenience, please download the sample from the link below, 
 
Please refer and use the below highlighted code(in yellow), 

 
public async Task Save(Microsoft.AspNetCore.Components.Web.MouseEventArgs args) 
{ 
    ... 
    var changed = await OrderGrid.GetBatchChangesAsync(); 
    //Employee newUpdatedList = new Employee(); 
    //newUpdatedList = Employees[0]; //Needs to be with the change 
    Employee newUpdatedList = new Employee() { Adress = Employees[0].Adress, ID = Employees[0].ID, 
Name = Employees[0].Name, Status = Employees[0].Status,  
Orders = new List<Order>() { new Order() { Example1 = Employees[0].Orders[0].Example1,Example2 = Employees[0].Orders[0].Example2, 
Example3 = Employees[0].Orders[0].Example3,Example4 = Employees[0].Orders[0].Example4 } } }; 
    ... 
} 


Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Marked as answer
Loader.
Up arrow icon