sfGrid check for changes/edits

Hi team,

Currently my datagrid edit mode is set to Inline/normal mode. I have the OnActionBegin event which checks if the user Added/Edited/Deleted a row.

The issue is when the user double clicks on a Row to update and does not change any column values and hit somewhere on the grid, the Args.RequestType is set to Save and Args.Action is set to Edit mode. My ActionBeginHandler checks the RequestType and Action types and updates the data source accordingly.


But since the user has not made any changes my database is getting updated. Is there a way to check if the user has indeed made any changes?

Below is my current code for  ActionBeginHandler method.


 public async void ActionBeginHandler(ActionEventArgs<ProductionLog> Args)

        {

            try

            {


                if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))

                {

                    Args.Data.UserModified = LocalStorageUser.UserName;

                    RowIndexValue = await productionLogsGrid.GetRowIndexByPrimaryKey(Args.RowData.ProductionLogID); //get Index value

                    if (Args.Action == "Add")

                    {

                        await ProductionLogDataService.AddProductionLog(Args.Data);

                        Toast[1].Content = "New Production Log added successfully.";

                        Toast[1].Title = "Add New Production Log";

                        await ToastObj.ShowAsync(Toast[1]);

                        await productionLogsGrid.Refresh();

                    }

                    else

                    {

                        await ProductionLogDataService.UpdateProductionLog(Args.Data);

                        Toast[1].Content = "Production Log updated successfully.";

                        Toast[1].Title = "Update Production Log";

                        await productionLogsGrid.Refresh();

                        await ToastObj.ShowAsync(Toast[1]);

                    }

                }

                if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))

                {

                    await ProductionLogDataService.DeleteProductionLog(Args.Data.ProductionLogID);

                    Toast[1].Content = "Production Log deleted successfully.";

                    Toast[1].Title = "Delete Production Log";

                    await ToastObj.ShowAsync(Toast[1]);

                    await productionLogsGrid.Refresh();

                }

                Description = string.Empty;

            }

            catch (Exception ex)

            {

                Toast[2].Content = "Unknown error occured while processing the request.Please contact administrator.";

                Toast[2].Title = "Unknown Error";

                await ToastObj.ShowAsync(Toast[2]);

            }

        }

My database should only be updated only when there is a change in the grid.


6 Replies

RS Renjith Singh Rajendran Syncfusion Team May 2, 2022 01:08 PM UTC

Hi Baba,


Greetings from Syncfusion support.


Query : does not change any column values and hit somewhere on the grid, the Args.RequestType is set to Save

This is the default behavior of grid editing. So, based on your scenario, we suggest you to compare the args.PreviousData and args.Data values you get from ActionEventArgs to achieve this requirement. You will get the changed values in args.Data and original value before change in args.PreviousData. So by checking these values you can conclude if any changes made in the edit form.


Please check this suggestion from your side and get back to us if you need further assistance.


Regards,

Renjith R



BG Baba Goud Gadiga replied to Renjith Singh Rajendran May 2, 2022 01:45 PM UTC

Hello Renjith,


The solution you proposed works if the entity has few properties. What if the entity has many properties? It would be cumbersome to check each and every property to see if it has changed. The datagrid should be able to tell me if any row value has changed or not. 


I see that in Batch edit mode when we change a cell value then we mark it as green, letting the user know a value has been changed. Why cant the grid do the same when the edit mode is set to Inline?  Please give a thought and let me know.


Thanks

Baba



RS Renjith Singh Rajendran Syncfusion Team May 3, 2022 08:57 AM UTC

Hi Baba,


We are further checking this scenario from our side. We will update you further details by May 5, 2022.


Until then we appreciate your patience.


Regards,

Renjith R



RS Renjith Singh Rajendran Syncfusion Team May 5, 2022 12:29 PM UTC

Hi Baba,


We have further analyzed this scenario. As informed in our previous update, this is the default behavior and provided solution is the recommended and possible solution to overcome this default behavior and achieve your requirement.


So please use the suggested solution from our previous update to achieve this requirement.


Regards,

Renjith R





NS Nibras Shawy June 15, 2023 09:20 AM UTC

Hi,
" The solution you proposed works if the entity has few properties. What if the entity has many properties? " What @ Baba said is correct. I think the answer of this issue is not complete. Please I have the same scenario and I don't want to check each property of my model !. Do you have another solution.



SP Sarveswaran Palani Syncfusion Team June 18, 2023 08:05 PM UTC

Hi Nibras,

Thanks for contacting Syncfusion support.

From your query, we suspect that you want to prevent the action of saving and editing when the value remains unchanged in Args.RequestType and Args.Action. To achieve this, we recommend two approaches.


  1. Firstly, you can check if the Args.Data and Args.PreviousData are equal and then skip the database call. This will prevent unnecessary updates when the value remains the same.


  1. Alternatively, you can utilize our built-in CloseEditAsync method, which cancels the edit state of the grid and discards any changes made without triggering the save action.


Please get back to us, if you have facing any issues in this.

Regards,
Sarvesh


Loader.
Up arrow icon