Skip to main content

GridCommandColumnCellDisplayTemplateContext.EditEnabled Property

Returns whether the Edit operation is available for a row from an asynchronous data source.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.2.dll

NuGet Package: DevExpress.Blazor

Declaration

public bool EditEnabled { get; }

Property Value

Type Description
Boolean

true if the edit operation is available; otherwise, false.

Remarks

A command column contains the predefined CRUD-related buttons (New, Edit, and Delete). You can define the column’s CellDisplayTemplate to implement custom command elements in cells that correspond to data rows.

Edit and Delete operations can be temporarily unavailable if you bind the Grid to an asynchronous data source (such as a Server Mode data source or GridDevExtremeDataSource). The EditEnabled and DeleteEnabled context parameters return false while the corresponding operation cannot be performed. Once Edit and Delete operations become available, these parameters return true.

In the template, use the EditEnabled and DeleteEnabled parameters to specify the enabled or disabled state for custom command elements. In the same way, you can use the NewEnabled parameter in the command column’s HeaderTemplate.

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

<DxGrid Data="InstantFeedbackSource"
        EditModelSaving="OnEditModelSaving"
        DataItemDeleting="OnDataItemDeleting"
        KeyFieldName="OrderId"
        EditMode="GridEditMode.EditRow"> 
    <Columns>
        <DxGridCommandColumn Width="150px">
            <HeaderTemplate>
                <DxButton Click="() => context.Grid.StartEditNewRowAsync()"
                          Enabled="context.NewEnabled" Text="Add" />
            </HeaderTemplate>
            <CellDisplayTemplate>
                @{
                    <DxButton Click="() => context.Grid.StartEditDataItemAsync(context.DataItem)"
                              Text="Edit" Enabled="context.EditEnabled"/>
                    <DxButton Click="() => context.Grid.ShowDataItemDeleteConfirmation(context.DataItem)"
                              Text="Delete" Enabled="context.DeleteEnabled"/>
                }
            </CellDisplayTemplate>
        </DxGridCommandColumn>
        <DxGridDataColumn FieldName="ShipName" />
        <DxGridDataColumn FieldName="ShipCountry" />
        <DxGridDataColumn FieldName="Freight"  />
        <DxGridDataColumn FieldName="OrderDate" />
    </Columns>
</DxGrid>

@code {
    EntityInstantFeedbackSource InstantFeedbackSource { get; set; }
    NorthwindContext Northwind { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        InstantFeedbackSource = new EntityInstantFeedbackSource(e => {
            e.KeyExpression = "OrderId";
            e.QueryableSource = Northwind.Orders;
        });
    }

    async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
        var editModel = (Order)e.EditModel;
        var dataItem = e.IsNew ? new Order() : Northwind.Orders.Find(editModel.OrderId);
        if (dataItem != null) {
            dataItem.ShipName = editModel.ShipName;
            dataItem.ShipCountry = editModel.ShipCountry;
            dataItem.Freight = editModel.Freight;
            dataItem.OrderDate = editModel.OrderDate;
            if (e.IsNew)
                await Northwind.AddAsync(dataItem);
            await Northwind.SaveChangesAsync();
        }
    }

    async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
        var dataItem = Northwind.Orders.Find((e.DataItem as Order).OrderId);
        if (dataItem != null) {
            Northwind.Remove(dataItem);
            await Northwind.SaveChangesAsync();
        }
    }

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

Tip

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

See Also