Skip to main content
A newer version of this page is available. .

DxGrid.EditMode Property

Specifies how users edit Grid data.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v22.1.dll

NuGet Package: DevExpress.Blazor

Declaration

[DefaultValue(GridEditMode.EditForm)]
[Parameter]
public GridEditMode EditMode { get; set; }

Property Value

Type Default Description
GridEditMode EditForm

A GridEditMode enumeration value.

Available values:

Name Description
EditForm

The Grid displays inline editors instead of the edited data row.

PopupEditForm

The Grid displays the edit form in a pop-up window.

EditRow

The Grid displays inline editors instead of the edited data row.

Remarks

The Grid allows users to edit its data. Use the EditMode option to choose one of the following edit modes:

GridEditMode.EditForm (Default)

The Grid displays the edit form instead of the edited data row.

Blazor Grid Edit Form

GridEditMode.PopupEditForm

The Grid displays the edit form in a pop-up window.

You can use the PopupEditFormCssClass property to assign a CSS class. The PopupEditFormHeaderText property allows you to change text in the edit form header.

Blazor Grid Popup Edit Form

The code below activates PopupEditForm mode.

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

<DxGrid Data="GridDataSource"
        EditModelSaving="OnEditModelSaving"
        DataItemDeleting="OnDataItemDeleting"
        KeyFieldName="EmployeeId"
        EditMode="GridEditMode.PopupEditForm">
    <Columns>
        <DxGridCommandColumn />
        <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;
        // Re-query a data item from the database.
        var dataItem = e.IsNew ? new Employee() : Northwind.Employees.Find(editModel.EmployeeId);

        // Assign changes from the edit model to the data item.
        if (dataItem != null) {
            dataItem.FirstName = editModel.FirstName;
            dataItem.LastName = editModel.LastName;
            dataItem.Title = editModel.Title;
            dataItem.HireDate = editModel.HireDate;
            // Post changes to the database.
            if (e.IsNew)
                await Northwind.AddAsync(dataItem);
            await Northwind.SaveChangesAsync();
            // Reload the entire Grid.
            GridDataSource = await Northwind.Employees.ToListAsync();
        }
    }

    async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
        // Re-query a data item from the database.
        var dataItem = Northwind.Employees.Find((e.DataItem as Employee).EmployeeId);
        if (dataItem != null) {
            // Remove the data item from the database.
            Northwind.Remove(dataItem);
            await Northwind.SaveChangesAsync();
            // Reload the entire Grid.
            GridDataSource = await Northwind.Employees.ToListAsync();
        }
    }

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

Run Demo: Grid - Edit Forms

GridEditMode.EditRow

The Grid displays inline editors instead of the edited row. Specify a common DataColumnCellEditTemplate or individual CellEditTemplate for each column to define an edit row’s content.

Blazor Grid Inline Edit Row

The code below activates EditRow mode.

    @inject NwindDataService NwindDataService

    <DxGrid @ref="Grid"
            Data="DataSource"
            PageSize="12"
            KeyFieldName="EmployeeId"
            ValidationEnabled="false"
            CustomizeEditModel="Grid_CustomizeEditModel"
            EditModelSaving="Grid_EditModelSaving"
            DataItemDeleting="Grid_DataItemDeleting"
            PopupEditFormCssClass="pw-800"
            EditMode="GridEditMode.EditRow">
        <Columns>
            <DxGridCommandColumn Width="140px" />
            <DxGridDataColumn FieldName="FirstName" MinWidth="80">
                <CellEditTemplate>
                    @{
                        var employee = (EditableEmployee)context.EditModel;
                    }
                    <DxTextBox @bind-Text="@employee.FirstName"></DxTextBox>
                </CellEditTemplate>
            </DxGridDataColumn>
            <DxGridDataColumn FieldName="LastName" MinWidth="80">
                <CellEditTemplate>
                    @{
                        var employee = (EditableEmployee)context.EditModel;
                    }
                    <DxTextBox @bind-Text="@employee.LastName"></DxTextBox>
                </CellEditTemplate>
            </DxGridDataColumn>
            <DxGridDataColumn FieldName="Title" MinWidth="100">
                <CellEditTemplate>
                    @{
                        var employee = (EditableEmployee)context.EditModel;
                    }
                    <DxTextBox @bind-Text="@employee.Title"></DxTextBox>
                </CellEditTemplate>
            </DxGridDataColumn>
            <DxGridDataColumn FieldName="HireDate" Width="10%" MinWidth="80">
                <CellEditTemplate>
                    @{
                        var employee = (EditableEmployee)context.EditModel;
                    }
                    <DxDateEdit @bind-Date="@employee.HireDate"></DxDateEdit>
                </CellEditTemplate>
            </DxGridDataColumn>
        </Columns>
    </DxGrid>

@code {
    IEnumerable<EditableEmployee> DataSource { get; set; }
    IGrid Grid { get; set; }

    protected override async Task OnInitializedAsync() {
        DataSource = await NwindDataService.GetEmployeesEditableAsync();
    }
    protected override async Task OnAfterRenderAsync(bool firstRender) {
        if(firstRender)
            await Grid.StartEditRowAsync(0);
    }
    void Grid_CustomizeEditModel(GridCustomizeEditModelEventArgs e) {
        if(e.IsNew) {
            var newEmployee = (EditableEmployee)e.EditModel;
            newEmployee.FirstName = "John";
            newEmployee.LastName = "Doe";
        }
    }
    async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e) {
        if(e.IsNew)
            await NwindDataService.InsertEmployeeAsync((EditableEmployee)e.EditModel);
        else
            await NwindDataService.UpdateEmployeeAsync((EditableEmployee)e.DataItem, (EditableEmployee)e.EditModel);
        await UpdateDataAsync();
    }
    async Task Grid_DataItemDeleting(GridDataItemDeletingEventArgs e) {
        await NwindDataService.RemoveEmployeeAsync((EditableEmployee)e.DataItem);
        await UpdateDataAsync();
    }
    async Task UpdateDataAsync() {
        DataSource = await NwindDataService.GetEmployeesEditableAsync();
    }
}

Run Demo: Edit Row Run Demo: Edit Row Input Validation

View Example: Grid - Inline Editing and Cell Edit Templates

Tip

For information on how to enable data editing and use edit-related options, refer to the following topic: Edit Data and Validate Input.

Implements

See Also