Clone a row just below position of it

Hello,

Ref. Below image. 

I have below grid with a clone button across each rows. On clicking the button, I need to clone that row and insert just below of that specific row. Can you reccommend what kind of Syncfusion grid would better support this feature? Any links talking about this implementation to save time?




3 Replies

RN Rahul Narayanasamy Syncfusion Team October 4, 2021 02:15 PM UTC

Hi Jaish, 

Greetings from Syncfusion. 

Query: Clone a row just below position of it - I have below grid with a clone button across each rows. On clicking the button, I need to clone that row and insert just below of that specific row. 

We have validated your query and you want to clone one row and insert that row below of the current row. We do not have direct support to achieve your requirement, but we can achieve your requirement using a custom solution.  

Here, we have rendered an button in a column using Column Template feature. Select any one row(which you want to clone and add) and click the corresponding row button. We have added one row below the selected record. Find the below code snippets and sample for your reference. 

 
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" AllowSelection="true" Toolbar="@(new List<string>() { "Add", "Update","Cancel" })"> 
    <GridEvents RowSelected="RowSelect" RowDeselected="RowDeselect" TValue="Order"></GridEvents> 
    <GridColumns> 
        <GridColumn HeaderText="Employee Image" TextAlign="TextAlign.Center" Width="120"> 
            <Template> 
                @{ 
                    <SfButton @onclick="Click" Content="Clone after selecting the row"></SfButton> 
                } 
            </Template> 
        </GridColumn> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" Visible="false" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        . . . 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<Order> Grid { get; set; } 
    public List<Order> Orders { get; set; } 
    public Order SelectedRecords { get; set; } 
    public double SelectedIndex { get; set; } 
    public void RowDeselect(RowDeselectEventArgs<Order> Args) 
    { 
        //remove the maintained value while deselecting the records 
        SelectedIndex = -1; 
        SelectedRecords = new Order(); 
    } 
    public void RowSelect(RowSelectEventArgs<Order> Args) 
    { 
        SelectedRecords = Args.Data; 
        SelectedIndex = Args.RowIndex; 
    } 
    . . . 
    private async Task Click(Microsoft.AspNetCore.Components.Web.MouseEventArgs args) 
    { 
        var selected = await Grid.GetSelectedRecordsAsync(); 
        var selecedCount = selected.Count; 
        if(selecedCount> 0) 
        { 
            //insert the duplicate record here 
            Orders.Insert((int)SelectedIndex + 1, SelectedRecords); 
            // refresh the grid data. 
            Grid.Refresh(); 
        } 
    } 
} 


Reference: 

Please let us know if you have any concerns. 

Regards, 
Rahul 



JM Jaish Mathews October 18, 2021 06:48 AM UTC

Thanks this helps.



RN Rahul Narayanasamy Syncfusion Team October 19, 2021 04:55 AM UTC

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


Loader.
Up arrow icon