Skip to main content

DxGrid.EditStart Event

Fires before the Grid starts editing a row.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.2.dll

NuGet Package: DevExpress.Blazor

Declaration

[Parameter]
public EventCallback<GridEditStartEventArgs> EditStart { get; set; }

Parameters

Type Description
GridEditStartEventArgs

An object that contains data for this event.

Remarks

The EditStart event occurs in the following cases:

Handle this event to create a custom response to the edit start action. Use the IsNew event argument to identify whether a new or existing data item is about to be edited. The DataItem property returns the processed data item. The Grid property allows you access the Grid and its extensive API.

You can set the Cancel event argument to true to prevent users from editing specific rows. The following code snippet prevents users from editing unselected rows:

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

<DxGrid Data="GridDataSource"
        EditModelSaving="OnEditModelSaving"
        KeyFieldName="EmployeeId"
        AllowSelectRowByClick="true"
        SelectionMode="GridSelectionMode.Single"
        @bind-SelectedDataItem="@SelectedDataItem"
        EditStart="OnEditStart"
        EditMode="GridEditMode.EditRow">
    <Columns>
        <DxGridCommandColumn DeleteButtonVisible="false" />
        <DxGridDataColumn FieldName="FirstName" />
        <DxGridDataColumn FieldName="LastName" />
        <DxGridDataColumn FieldName="Title" />
        <DxGridDataColumn FieldName="HireDate" />
    </Columns>
</DxGrid>

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

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

    async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
        var editModel = (Employee)e.EditModel;
        // Assign changes from the edit model to the data item.
        if (e.IsNew)
            await Northwind.AddAsync(editModel);
        else
            e.CopyChangesToDataItem();
        // Post changes to the database.
        await Northwind.SaveChangesAsync();
        // Reload the entire Grid.
        GridDataSource = await Northwind.Employees.ToListAsync();
     }

    async Task OnEditStart(GridEditStartEventArgs e) {
        if (!e.IsNew && !e.Grid.IsDataItemSelected(e.DataItem))
            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