Iteration in datagrid to get updated cell value. Total and Aggregation.

Hi team 


I'm using batch saving. is there is any way to loop within datagrid and get the UpdatedCell value.. I try hereunder codes. 

But it only gets the sourcedata value instead of  Updated cell value. Or there must be other way to get my total amount in batch saving.

 

 public double TotalAmount;

  public async Task ComputeDetail()

        {

            TotalCostValue = QuantityValue * UnitCostValue;

            await GridDetail.UpdateCell(RowIndexDetail, "TotalCost", TotalCostValue);

    

            var RowsData = GridDetail.CurrentViewData;

            foreach (SODetailEdit row in RowsData)

            {

              TotalAmount = TotalAmount + row.TotalCost;

            }


            }

        }


 public void CellSavedHandler(CellSaveArgs<SODetailEdit> args)

        { 

           if (args.ColumnName == "Quantity")

            {

                QuantityValue = (double)args.Value;

                ComputeDetail();

            }


            if (args.ColumnName == "UnitCost")

            {

                UnitCostValue = (double)args.Value;


                ComputeDetail();

            }


        }


Warm Regards Tyrone


7 Replies

RN Rahul Narayanasamy Syncfusion Team July 15, 2021 05:09 AM UTC

Hi Tyrone, 

Greetings from Syncfusion. 

Query: in datagrid to get updated cell value - it only gets the sourcedata value instead of  Updated cell value. Or there must be other way to get my total amount in batch saving. 

We have validated your query and you want to get the updated cell values in batch mode of editing. Since you have tried to get the updated values(before updating) using CurrentViewData. It will reflect the updated cell changes after saving the values. 

You can get the updated cell values by using GetBatchChangesAsync method of the Grid. You can get all the added, deleted and updated values using this method. Find the below code snippets and screenshot for your reference. 
 
public async Task ComputeDetail() 
    { 
        TotalCostValue = QuantityValue * UnitCostValue; 
        . . . 
 
        var ChangedData = await Grid.GetBatchChangesAsync(); 
 
        await Grid.UpdateCell(RowIndexDetail, "TotalCost", TotalCostValue);   
        . . . 
 
    } 

[Screenshot] 
 

Reference: 

Please let us know if you have any concerns. 

Regards, 
Rahul 
 



TY Tyrone July 17, 2021 11:55 AM UTC

Thanks Rahul for the immediate reply,


By the way, How could I get the rows that has no changes?





RN Rahul Narayanasamy Syncfusion Team July 19, 2021 12:49 PM UTC

Hi Tyrone, 

Thanks for the update. 

Query: By the way, How could I get the rows that has no changes? 

We have validated your  query and you want to get the unchanged record values in batch  editing. You can get the unchanged record details by using below way. We have iterated the CurrrentView records and Changed records to get the unchanged record values. Find the below code snippets for your reference. 

 
<button @onclick="Get"> Get UnChanged</button> 
 
<div>@UnChanged.Count</div> 
<SfGrid @ref="Grid" DataSource="@Orders" AllowSelection="true" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315"> 
    <GridEvents OnCellSave="CellSaveHandler" CellSelecting="CellSelectingHandler" TValue="Order"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings> 
    <GridSelectionSettings Mode=SelectionMode.Cell></GridSelectionSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        . ..  
 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<Order> Grid; 
    . ..  
 
    public List<Order> UnChanged { get; set; } = new List<Order>(); 
    public async Task Get() 
    { 
        UnChanged.Clear(); 
        var ChangedData = await Grid.GetBatchChangesAsync();    //get batch changes 
        var CurrentView = await Grid.GetCurrentViewRecordsAsync();    // get current view data 
 
        for (var i = 0; i < ChangedData.ChangedRecords.Count; i++) 
        { 
            if (UnChanged.Count > 1) 
            { 
                UnChanged = UnChanged.Where(x => x.OrderID != ChangedData.ChangedRecords[i].OrderID).ToList(); 
            } 
            else 
            { 
                UnChanged = CurrentView.Where(x => x.OrderID != ChangedData.ChangedRecords[i].OrderID).ToList(); 
            } 
        } 
 
    } 
 
    . . . 
} 


Please let us know if you have any concerns. 

Regards, 
Rahul 



TY Tyrone July 20, 2021 04:02 PM UTC

Thanks Rahul. This is great


Lastly, How could I get the column value for each Row? 


Since  Reactive aggregate is not yet available..  I'm planning to get the totals from iteration of changed and unchanged data.. 


example: 

 for (var i = 0; i < ChangedData.ChangedRecords.Count; i++)


     Totalcost+ = TotalCostFromThecell (changed)

}


 for (var i = 0; i < Unchanged.Count; i++)


     Totalcost+ = TotalCostFromThecell (unchanged)

}


Or there must be other way to get the total from batchmode. 



RN Rahul Narayanasamy Syncfusion Team July 21, 2021 12:02 PM UTC

Hi Tyrone, 

Thanks for the update. 

Query: Lastly, How could I get the column value for each Row? 

We have validated your query and you can get the particular column values from the record details(ChangedData and UnChanged). Find the below code snippets for your reference. 

 
<button @onclick="Get"> Get UnChanged</button> 
 
<button @onclick="GetTotal">Get Total Value</button> 
 
<div>@UnChanged.Count</div> 
<div>Total Cost - @Totalcost</div> 
. . . 
 
@code{ 
    SfGrid<Order> Grid; 
    . . . 
    public double? Totalcost { get; set; } = 0; 
    public async Task GetTotal() 
    { 
        Totalcost = 0; 
        var ChangedData = await Grid.GetBatchChangesAsync(); 
        for (var i = 0; i < ChangedData.ChangedRecords.Count; i++) 
        { 
            Totalcost += ChangedData?.ChangedRecords[i].TotalCost; 
        } 
        for (var i = 0; i < UnChanged.Count; i++) 
        { 
            Totalcost += UnChanged[i].TotalCost; 
        } 
    } 
 
    . . . 
} 


Currently we don’t have reactive aggregate support. We have already considered it as a usability improvement and logged the improvement task “Reactive aggregate support for datagrid” for the same. Thank you for taking the time to request this improvement and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technological feasibility and Product Development Life Cycle ) and including the improvement feature in our upcoming volume 4, 2021 release.  
   
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.  
   
  
Until then we appreciate your patience.   
  
Disclaimer: The feature inclusion date is tentative and not a commitment from our side                                               
 
Regards, 
Rahul 



TY Tyrone July 23, 2021 03:38 AM UTC

Thanks Rahul,


Your code helps me a lot..  I made some finalization in the code. 


  public List<SODetailEdit> UnChanged { get; set; } = new List<SODetailEdit>();

  public TotalAmount? Totalcost { get; set; } = 0; 

  public async Task GetTotal() 

{

 TotalAmount = 0;


            UnChanged.Clear();

            var ChangedData = await GridDetail.GetBatchChangesAsync(); //get batch changes

            var CurrentView = await GridDetail.GetCurrentViewRecordsAsync(); // get current view data


            for (var i = 0; i < CurrentView.Count; i++)

            {

                bool ISexists = false;


                //Edit

                for (var x = 0; x < ChangedData.ChangedRecords.Count; x++)


                {

                   if (CurrentView[i]. SODetailID == ChangedData.ChangedRecords[x].SODetailID)

                    {

                        ISexists = true;

                    }


                }



                //Delete

                for (var x = 0; x < ChangedData.DeletedRecords.Count; x++)


                {

                    if (CurrentView[i].SODetailID == ChangedData.DeletedRecords[x].SODetailID)

                    {

                        ISexists = true;

                    }


                }


                //Adding Unchanged

                if (ISexists == false)

                {

                    UnChanged.Add(CurrentView[i]);

                }


            }


       //  Aggregation

            //Added

            for (var i = 0; i < ChangedData.AddedRecords.Count; i++)

            {

                TotalAmount += ChangedData.AddedRecords[i].Quantity;

            }


            //Edited

            for (var i = 0; i < ChangedData.ChangedRecords.Count; i++)

            {

                TotalAmount += ChangedData.ChangedRecords[i].Quantity;

            }


            //Unchanged

            for (var i = 0; i < UnChanged.Count; i++)

            {

                TotalAmount += UnChanged[i].Quantity;

            }


            StateHasChanged();

}


Warm Regards,




RN Rahul Narayanasamy Syncfusion Team July 26, 2021 04:31 AM UTC

Hi Tyrone, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful. Please get back to us if you need further assistance. 
 
Regards, 
Rahul 


Loader.
Up arrow icon