DxGrid.EditCanceling Event
Fires before the edit form closes and discards changes.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v21.2.dll
NuGet Package: DevExpress.Blazor
Declaration
[Parameter]
public EventCallback<GridEditCancelingEventArgs> EditCanceling { get; set; }
Parameters
Type | Description |
---|---|
GridEditCancelingEventArgs | A GridEditCancelingEventArgs object that contains data for this event. |
Remarks
The Grid allows users to edit its data either by using the edit form or by showing the edit form in a pop-up window.
Tip
For information on how to enable data editing, refer to the following topic: Edit Data and Validate Input.
The EditCanceling
event occurs in the following cases:
- When a user clicks the Cancel button in the edit form.
- When the CancelEditAsync method is called.
Handle this event to create a custom response to edit cancel action. Use the IsNew event argument to identify whether the edit form is used for a new or existing row. The DataItem and EditModel properties return the processed data item and edit model. You can also access the Grid object and use its members to obtain additional information about the Grid.
Another option is to set the Cancel property to true
to prevent the cancel action.
The following example demonstrates how to prevent the cancel action in the edit from used 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">
@{
var employee = (Employee)editFormContext.EditModel;
}
<DxFormLayout>
<DxFormLayoutItem Caption="First Name:">
<DxTextBox @bind-Text="@employee.FirstName" />
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Last Name:">
<DxTextBox @bind-Text="@employee.LastName" />
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Title:">
<DxTextBox @bind-Text="@employee.Title" />
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Hire Date:">
<DxDateEdit @bind-Date="@employee.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;
var dataItem = e.IsNew ? new Employee() : Northwind.Employees.Find(editModel.EmployeeId);
if (dataItem != null) {
dataItem.FirstName = editModel.FirstName;
dataItem.LastName = editModel.LastName;
dataItem.Title = editModel.Title;
dataItem.HireDate = editModel.HireDate;
if (e.IsNew)
await Northwind.AddAsync(dataItem);
await Northwind.SaveChangesAsync();
GridDataSource = await Northwind.Employees.ToListAsync();
}
}
async Task OnEditCanceling(GridEditCancelingEventArgs e) {
if (e.IsNew)
e.Cancel = true;
}
public void Dispose() {
Northwind?.Dispose();
}
}