Skip to main content

DxGrid.EditCanceling Event

Fires before the Grid cancels the edit operation and discards changes.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.2.dll

NuGet Package: DevExpress.Blazor

Declaration

[Parameter]
public EventCallback<GridEditCancelingEventArgs> EditCanceling { get; set; }

Parameters

Type Description
GridEditCancelingEventArgs

An object that contains data for this event.

Remarks

The EditCanceling event occurs in the following cases:

  • A user presses Esc in the pop-up edit form.
  • A user clicks the Cancel button in the edit form or command column.
  • In EditCell mode, a user presses Esc when an editor in the focused data cell is hidden.
  • In EditCell mode, a user performs a data shaping operation that hides the edited row from the view and row validation fails.
  • The CancelEditAsync method is called.

Handle this event to create a custom response to edit the cancel action. Use the IsNew event argument to identify whether a new or existing data item is being edited. The DataItem and EditModel properties return the processed data item and edit model. The Grid property allows you to access the Grid and its extensive API.

You can set the Cancel property to true to prevent the cancel action. The following example prevents the cancel action for new rows:

@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable

<DxGrid Data="GridDataSource"
        EditModelSaving="OnEditModelSaving"
        KeyFieldName="EmployeeId"
        EditCanceling="OnEditCanceling">
    <Columns>
        <DxGridCommandColumn DeleteButtonVisible="false" />
        <DxGridDataColumn FieldName="FirstName" />
        <DxGridDataColumn FieldName="LastName" />
        <DxGridDataColumn FieldName="Title" />
        <DxGridDataColumn FieldName="HireDate" />
    </Columns>
    <EditFormTemplate Context="editFormContext">
        <DxFormLayout>
            <DxFormLayoutItem Caption="First Name:">
                @editFormContext.GetEditor("FirstName")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Last Name:">
                @editFormContext.GetEditor("LastName")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Title:">
                @editFormContext.GetEditor("Title")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Hire Date:">
                @editFormContext.GetEditor("HireDate")
            </DxFormLayoutItem>
        </DxFormLayout>
    </EditFormTemplate>
</DxGrid>

@code {
    IEnumerable<object> GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }

    protected override async Task OnInitializedAsync() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        GridDataSource = await Northwind.Employees.ToListAsync();
    }

    async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
        var editModel = (Employee)e.EditModel;
        if (e.IsNew)
            await Northwind.AddAsync(editModel);
        else
            e.CopyChangesToDataItem();
        await Northwind.SaveChangesAsync();
        GridDataSource = await Northwind.Employees.ToListAsync();
    }

    async Task OnEditCanceling(GridEditCancelingEventArgs e) {
        if (e.IsNew)
            e.Cancel = true;
    }

    public void Dispose() {
        Northwind?.Dispose();
    }
}

For information on how to enable data editing, refer to the following topic: Editing and Validation in Blazor Grid.

See Also