Skip to main content

DxGridCommandColumn.CellEditTemplate Property

Specifies a template used to display the command column’s edit cell.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.2.dll

NuGet Package: DevExpress.Blazor

Declaration

[Parameter]
public RenderFragment<GridCommandColumnCellEditTemplateContext> CellEditTemplate { get; set; }

Property Value

Type Description
RenderFragment<GridCommandColumnCellEditTemplateContext>

A template for the command column’s edit cell.

Remarks

The command column displays predefined New, Edit, and Delete command buttons for data rows in display mode. In EditRow and EditCell edit modes, this column displays Save and Cancel buttons for the edited row. In the filter row, the column displays the Clear button.

Blazor Grid Command Column

Read Tutorial: Edit Data in Blazor Grid Read Tutorial: Templates in Blazor Grid

You can define the CellEditTemplate to display custom command elements for the edited row. Use the template’s context parameter to access CommandColumn and DataItem objects. The Grid object allows you to access the Grid component and call the following edit-related methods:

SaveChangesAsync()
Triggers validation and raises the EditModelSaving event if validation succeeds. The method immediately raises this event if validation is disabled.
CancelEditAsync()
Cancels row editing and discards changes.

Specify the CellDisplayTemplate to display custom command elements for cells in display mode. Use HeaderTemplate and FilterRowCellTemplate to display custom content in the command column header and filter row.

The following example hides the predefined Save and Cancel buttons and implements custom buttons within the edit row:

Blazor Grid Edit Row with Custom Buttons

@inject NwindDataService NwindDataService

<DxGrid @ref="MyGrid"
        PageSize="5"
        Data="GridDataSource"
        EditModelSaving="OnEditModelSaving"
        DataItemDeleting="OnDataItemDeleting"
        KeyFieldName="EmployeeId"
        EditMode="GridEditMode.EditRow" >
    <Columns>
        <DxGridCommandColumn Width="200px" CancelButtonVisible="false" SaveButtonVisible="false">
            <CellEditTemplate>
                <DxButton Click="@(() => MyGrid.SaveChangesAsync())" Text="Update" />
                <DxButton Click="@(() => MyGrid.CancelEditAsync())" Text="Discard" />
            </CellEditTemplate>
        </DxGridCommandColumn>
        <DxGridDataColumn FieldName="FirstName" />
        <DxGridDataColumn FieldName="LastName" />
        <DxGridDataColumn FieldName="Title" />
        <DxGridDataColumn FieldName="HireDate" />
    </Columns>
</DxGrid>

@code {
    IEnumerable<object> GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }
    IGrid MyGrid { 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();
        }
    }
}

Implements

See Also