Handling add new with primary key violation

Hi

I am trying to prevent the user from adding a new record with an existing primary key

Using EditMode.Dialog in SfDataGrid

Trying to handle it in OnActionComplete handler

If I check the entered data for existence in existing data and set args.Cancel = true, it does not cancel the add operation ? It is added to the list anyway.


Peter


3 Replies 1 reply marked as answer

SK Sanjay Kumar Suresh Syncfusion Team February 13, 2025 09:13 AM UTC

Hi Peter Rasmussen,


It seems like you want to prevent adding the record if the same primary key values are entered. Based on your requirements, we suggest using the RowUpdating and RowEditing events to achieve your requirements. Kindly refer to the code snippet and sample below for reference.


Concern Code Snippet:


public void RowUpdatingHandler(RowUpdatingEventArgs<Order> args)

    {

 

 

        var IsPresent = Orders.Where(or => or.OrderID == args.Data.OrderID).FirstOrDefault();

 

        if (IsPresent != null && flag !=true )

 

        {

            args.Cancel = true;

 

        }

        else

        {

            flag = false;

        }

    }

    public void RowEditingHandler(RowEditingEventArgs<Order> args)

    {

        flag = true;

    }

 


We have made a simple sample to achieve your requirement please check the below sample for more reference.


Sample:

https://blazorplayground.syncfusion.com/embed/VNhoZBWKLlUwFiwf?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


However, we would like to inform you that the ActionBegin event for CRUD actions will soon be deprecated. We have introduced separate events for each CRUD operation, and it is recommended that you handle specific actions with the new events instead. In your case, we suggest using the RowUpdating event to achieve your requirement.

References:



New Event Information

Event Name

Argument Name

Properties

Description

RowCreating

RowCreatingEventArgs

Cancel, Data, Index, EditContext

Gets or sets the event callback that is raised before the add action is performed in the grid.

RowCreated

RowCreatedEventArgs

Data, Index, EditContext

Gets or sets the event callback that is raised after the add action is performed in the grid.

RowUpdating

RowUpdatingEventArgs

Cancel, IsShiftKeyPressed, KeyCode, Data, Index, PreviousData, PrimaryKeys, PrimaryKeyValue

Gets or sets the event callback that is raised before the save action is performed in the grid.

RowUpdated

RowUpdatedEventArgs

Data, Index, PreviousData, PrimaryKeys, PrimaryKeyValue

Gets or sets the event callback that is raised after the save action is performed in the grid.

RowDeleting

RowDeletingEventArgs

Cancel, Datas, PrimaryKeys

Gets or sets the event callback that is raised before the delete action is performed in the grid.

RowDeleted

RowDeletedEventArgs

Datas, PrimaryKeys

Gets or sets the event callback that is raised after the delete action is performed in the grid.

EditCanceling

EditCancelingEventArgs

Cancel, Data, PreviousData, PrimaryKeys, Index

Gets or sets the event callback that is invoked before the cancel action is performed in the grid, specifically when using normal and dialog edit modes.

EditCanceled

EditCanceledEventArgs

Data, PreviousData, PrimaryKeys, Index

Gets or sets the event callback that is invoked after the cancel action is performed in the grid, specifically when using normal and dialog edit modes.

OnRowEditStart

OnRowEditStartEventArgs

Cancel, PreventDataClone

Gets or sets the event callback that is raised before an editing action is performed in the grid.

RowEditing

RowEditingEventArgs

Cancel, PrimaryKeys, PrimaryKeyValue, Data, Index, EditContext, ForeignKeyData

Gets or sets the event callback that is raised before the edit action is performed in the grid.

RowEdited

RowEditedEventArgs

PrimaryKeys, PrimaryKeyValue, Data, Index, EditContext, ForeignKeyData

Gets or sets the event callback that is raised after the edit action is performed in the grid.


If you have any further questions or need additional assistance, feel free to ask!


Regards,

Sanjay Kumar Suresh


Marked as answer

PR Peter Rasmussen February 13, 2025 12:11 PM UTC

Thanks Sanjay

That works like a charm

Another problem in my first try was that in the ActionCompletedHandler, the duplicate had already bee added to the list. Your way works perfectly; it never gets added.


Thanks

Peter



SK Sanjay Kumar Suresh Syncfusion Team February 14, 2025 06:06 AM UTC

Thanks for the update.


We are happy to hear that the provided solution was helpful. Please get back to us if you need any other assistance.


Loader.
Up arrow icon