Grid rows editing with empty identity

I have grid with columns (DataSource is object from DB context ( DataSource="trainingCycleTemplate.CycleTemplates")):
<GridColumns>
                        <GridColumn Field="@nameof(CycleTemplateModel.Id)" IsIdentity="true" IsPrimaryKey="true" Visible="false"></GridColumn>
                        <GridColumn Field="@nameof(CycleTemplateModel.Name)"></GridColumn>
                        <GridColumn Field="@nameof(CycleTemplateModel.Percent)"></GridColumn>
                        <GridColumn Field="@nameof(CycleTemplateModel.Order)"></GridColumn>
</GridColumns>

The problem is: when I'm adding new records and then I need to change it. All new records is changed, because id =0.
If I save everything in DB. Then ID is populated with identity values and everything is ok. 
What should I do to have a possibility change record before saving to DB.



3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team February 16, 2021 09:38 AM UTC

Hi Aleksandrs, 

Greetings from Syncfusion support. 

We suggest you to bind the OnActionBegin event to Grid. You can customize the data to be added in this event handler using the args.Data based on RequestType and Action values. 
 
Please refer and use the codes below, 
 
 
@using Action = Syncfusion.Blazor.Grids.Action
<GridEvents OnActionBegin="OnActionBegin" TValue="Order"></GridEvents>
public void OnActionBegin(ActionEventArgs<Order> args) 
{ 
    if(args.RequestType.Equals(Action.Save) && args.Action == "Add") 
    { 
        //args.Data you will get added data info, you can modify the Id value based on your requirement 
    } 
} 



Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



AL Aleksandrs February 16, 2021 12:06 PM UTC

The problem is that ID field should be assigned by EF when I save a data to DB.
 So I can't assign any values to the ID field.


RS Renjith Singh Rajendran Syncfusion Team February 17, 2021 09:17 AM UTC

Hi Aleksandrs, 

Based on your scenario, it is recommend to use Custom way of binding data to Grid. With this support you can handle the CRUD operations in Grid in a custom way by overriding our default CRUD methods Insert/InsertAsync, Remove/RemoveAsync, Update/UpdateAsync. You can update your datasource in db and customize the CRUD actions based on your requirement in these methods and return the value to update in Grid and load from db in Read/ReadAsync method to display changes in Grid.  
References :  

We have prepared a sample based on this scenario for your convenience. Please download the sample from the link below, 

Note : We have handled only Add operation(Insert method) codes in above sample. And also ensure to change the db path based on your local path in OrderContext.cs before running the above sample. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 


Marked as answer
Loader.
Up arrow icon