Skip to main content

DxGrid.DataColumnCellDisplayTemplate Property

Specifies a common template used to display all data column cells in the Grid.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.1.dll

NuGet Package: DevExpress.Blazor

Declaration

[Parameter]
public RenderFragment<GridDataColumnCellDisplayTemplateContext> DataColumnCellDisplayTemplate { get; set; }

Property Value

Type Description
RenderFragment<GridDataColumnCellDisplayTemplateContext>

The template for all data column cells.

Remarks

The DataColumnCellDisplayTemplate allows you to specify custom content and change the appearance of cells in all Grid data columns. To define a template for individual Grid data columns, use the DxGridDataColumn.CellDisplayTemplate.

The DataColumnCellDisplayTemplate accepts a GridDataColumnCellDisplayTemplateContext object as the context parameter. You can use the parameter’s members to get information about a column cell (DataColumn, Value, DisplayText, HighlightedDisplayText, VisibleIndex). You can also access the Grid object and use its members to obtain additional information about the Grid.

The following example customizes appearance of column cells to distinguish between odd and even rows:

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

<style>
    .my-style {
        color: blue;
        font-weight: bold;
    }
    .my-date-style {
        color: mediumblue;
        font-weight: bold;
    }
</style>

<DxGrid Data="@Data">
    <Columns>
        <DxGridDataColumn FieldName="FirstName" />
        <DxGridDataColumn FieldName="LastName" />
        <DxGridDataColumn FieldName="Title" />
        <DxGridDataColumn FieldName="BirthDate" />
        <DxGridDataColumn FieldName="HireDate" />
    </Columns>
    <DataColumnCellDisplayTemplate>
        @{
            if (context.VisibleIndex % 2 == 1) {
                if (context.DataColumn.FieldName.Contains("Date")) {
                    <span class="my-date-style">@context.DisplayText</span>
                } else {
                    <span class="my-style">@context.DisplayText</span>
                }
            } else {
                <span>@context.DisplayText</span>
            }
        }
    </DataColumnCellDisplayTemplate>
</DxGrid>

@code {
    IEnumerable<Employee> Data { get; set; }
    NorthwindContext Northwind { get; set; }
    Employee CurrentEmployee { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        Data = Northwind.Employees
            .ToList();
    }

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

Blazor Grid Column Cell Display Template

For more information about templates in the Grid component, refer to the following topic: Templates in Blazor Grid.

Cell Editing Specifics

During edit operations, the Grid applies user changes only to the edit model and keeps the data item unchanged. DataItem, DisplayText, and Value context parameters return information about the data item, not the edit model. As a result, values of these properties do not reflect unsaved changes.

When the EditMode is set to CellEdit, only one grid cell can be in edit mode while others are in display mode. The CellDisplayTemplate affects contents of cells in display mode, including cells with modified values. To display unsaved changes in the CellDisplayTemplate, use the edit model’s field values instead of properties of the template’s context parameter:

@inject NwindDataService NwindDataService

<DxGrid @ref="Grid"
        Data="DataSource"
        KeyFieldName="EmployeeId"
        EditMode="GridEditMode.EditCell"
        EditModelSaving="Grid_EditModelSaving">
    <Columns>
        <DxGridDataColumn FieldName="FirstName" />
        <DxGridDataColumn FieldName="LastName" />
        <DxGridDataColumn FieldName="BirthDate" />
        <DxGridDataColumn FieldName="HireDate" />
    </Columns>
    <DataColumnCellDisplayTemplate Context="displayContext">
        @{
            string value;
            if (displayContext.Grid.IsEditingRow(displayContext.VisibleIndex)) {
                var editModel = (Employee)displayContext.Grid.GetEditContext().Model;
                var fieldName = displayContext.DataColumn.FieldName;
                value = editModel.GetType().GetProperty(fieldName).GetValue(editModel).ToString();
            }
            else
                value = displayContext.DisplayText;
        }
        <span>@value</span>
    </DataColumnCellDisplayTemplate>
</DxGrid>

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

    protected override async Task OnInitializedAsync() {
        DataSource = await NwindDataService.GetEmployeesAsync();
    }
    async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e) {
        var editableEmployee = (Employee)e.EditModel;
        await NwindDataService.UpdateEmployeeAsync((Employee)e.DataItem, editableEmployee);
        DataSource = await NwindDataService.GetEmployeesAsync();
    }
}
See Also