Update multiple Row based on external input or parameter.

Hi team,

I have just seen this in  another platform thread. Is there is a way to do this in blazor?


<script> 
    function select(args) { //select event of dropdown 
        var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0]; 
        var records = grid.getSelectedRecords(); 
        for (var i = 0; i < records.length; i++) { 
            grid.updateCell(i, "CustomerID", args.itemData.CustomerID); 
        }                        //get the rowindex by using primary key   //specific column name     //value 
    } 
</script> 


Warm Regards,

Tyrone

5 Replies

VN Vignesh Natarajan Syncfusion Team July 19, 2021 03:45 AM UTC

Hi Tyrone,  
 
Thanks for contacting Syncfusion support.  
 
Query: “I have just seen this in  another platform thread. Is there is a way to do this in blazor? 
 
Yes, we have achieved the similar requirement in Blazor platform using UpdateCellAsync method of Grid. We have used GetSelectedRowAsync() method and GetRowIndexByPrimarykeyAsync() to find the selected records and its corresponding indexes.  
 
Refer the below code example.  
 
<SfTextBox @bind-Value="@Text"></SfTextBox> 
<SfButton OnClick="Clicked" Content="Update Selected"></SfButton> 
  
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add""Delete""Update""Cancel" })" Height="315"> 
    <GridSelectionSettings Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></GridSelectionSettings> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })" Type="ColumnType.Number" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" ValidationRules="@(new ValidationRules{ Required=true})" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"></GridColumn> 
    </GridColumns> 
</SfGrid> 
  
@code{ 
    public List<Order> Orders { getset; } 
  
    public string Text { getset; } 
  
    public SfGrid<Order> Grid { getset; } 
  
    public async Task Clicked() 
    { 
        var getSelected = await Grid.GetSelectedRecordsAsync(); 
        foreach (var sel in getSelected) 
        { 
            var index await Grid.GetRowIndexByPrimaryKeyAsync(sel.OrderID); 
            await Grid.UpdateCellAsync(index, "ShipCountry", Text); 
        } 
    } 
 
 
For your convenience we have prepared a sample which can be downloaded from below  
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 



TY Tyrone July 19, 2021 11:45 AM UTC

Thanks Vignesh for the immediate response.

btw, another questions

1) Is there is a way to get the index without the ID? 

var index await Grid.GetRowIndexByPrimaryKeyAsync(sel.OrderID); 

My Id is auto generated from EFCore after batchsaving.  and Newly added lineitem has 0 yet ID.


2)  How can I get the all of the lineitems without selecting them all? 

 var getSelected = await Grid.GetSelectedRecordsAsync(); 

  

Warm regards,

Tyrone



VN Vignesh Natarajan Syncfusion Team July 20, 2021 03:36 AM UTC

Hi Tyrone, 
 
Thanks for the update. 
 
Query: “Is there is a way to get the index without the ID? && How can I get the all of the lineitems without selecting them all?  
 
We have analyzed your query and we suggest you to achieve your requirement to get all the line items in Grid using GetCurrentViewRecordsAsync() method of Grid. Similarly from the current view records we find indexes of specific records by using C# IndexOf method.  
 
Refer the below code example.  
 
public async Task Clicked() 
{ 
    var getSelected = await Grid.GetCurrentViewRecordsAsync(); 
    foreach (var sel in getSelected) 
    { 
        var index = getSelected.IndexOf(sel); 
        await Grid.UpdateCellAsync(index, "ShipCountry", Text); 
    } 
} 
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan  



TY Tyrone July 20, 2021 04:07 PM UTC

Thanks Vignesh.


By the way, is there is a way to get the cell value for each  row in Grid.GetCurrentViewRecordsAsync();

I'm planning to use it to get the total (ex. totalAmount) since reactive aggregate is not yet available in batchmode..


Warm Regards,


Tyrone



VN Vignesh Natarajan Syncfusion Team July 21, 2021 03:10 AM UTC

  
Hi Tyrone,  

Thanks for the update.  

Query: “By the way, is there is a way to get the cell value for each  row in Grid.GetCurrentViewRecordsAsync(); 

Yes, you can get the entire current view data of Grid using GetCurrentViewRecordsAsync method. From that we can filter the particular record and finds value using column name. Refer the below code example and screenshot for your reference 

public async Task Clicked() 
    { 
        var getSelected = await Grid.GetCurrentViewRecordsAsync(); 
        var total = getSelected[0]; 
        var val = total.CustomerID; 
        foreach (var sel in getSelected) 
        { 
            var index = getSelected.IndexOf(sel); 
            await Grid.UpdateCellAsync(index, "ShipCountry", Text); 
        } 
    } 



 

Please get back to us if you have further queries.  

Regards, 
Vignesh Natarajan 



Loader.
Up arrow icon