Can we do Batch Insert in database In Grid Control ?

Hi All,

Can we do the batch insert in database similar to Batch Update in Grid Control ? if yes then could you please send me the sample code snippet ?

1 Reply 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team December 18, 2020 01:56 PM UTC

Hi Chandradev, 

Greetings from Syncfusion. 

We suggest you to achieve your requirement (to save the changes in Database) using OnBatchSave event of the Grid. This event will be triggered when saving all the changes (as a whole) in Grid. In the event argument we can get the changes performed in Grid like changed record, added record, deleted records etc. Using this we can update the changes in Database. 

Refer the below code example.  

<SfGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add""Delete""Update""Cancel" })"> 
    <GridEvents OnBatchSave="OnSave" TValue="Order"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch"></GridEditSettings> 
. . . . . . 
</SfGrid> 
 
@code{ 
    public List<Order> Orders { getset; } 
    public void OnSave(BeforeBatchSaveArgs<Order> Args) 
    { 
        var BatchChanges = Args.BatchChanges; 
        if(BatchChanges.AddedRecords.Count > 0) 
        { 
            //Insert data into your database 
        } 
        if(BatchChanges.ChangedRecords.Count > 0) 
        { 
            //update changes into your database 
        } 
        if(BatchChanges.DeletedRecords.Count > 0) 
        { 
            //delete record from your database 
        } 
    } 

Refer our UG documentation for your reference 


If you are using Custom way of binding, then please refer the below documentation for reference. 


Regards, 
Rahul 


Marked as answer
Loader.
Up arrow icon