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

GridCommandColumnHeaderTemplateContext.NewEnabled Property

Returns whether the Add operation is available for an asynchronous data source.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public bool NewEnabled { get; }

Property Value

Type Description
Boolean

true if the add 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 HeaderTemplate to implement a custom New element in the column header.

The Add operation can be temporarily unavailable if you bind a Grid to an asynchronous data source (such as a Server Mode data source or GridDevExtremeDataSource). The NewEnabled context parameter returns false when the Add operation cannot be performed. Once this operation becomes available, this parameter returns true.

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

@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: Edit Data in Blazor Grid.

See Also