blazor-devexpress-dot-blazor-dot-dxgriddatacolumn-86b34688.md
Specifies a template used to display the column’s cells.
Namespace : DevExpress.Blazor
Assembly : DevExpress.Blazor.v25.2.dll
NuGet Package : DevExpress.Blazor
[Parameter]
public RenderFragment<GridDataColumnCellDisplayTemplateContext> CellDisplayTemplate { get; set; }
| Type | Description |
|---|---|
| RenderFragment<GridDataColumnCellDisplayTemplateContext> |
The template for the column’s cells.
|
The CellDisplayTemplate property allows you to specify custom content and change the appearance of cells in individual columns. To define a common template for cells of all Grid columns, use the DxGrid.DataColumnCellDisplayTemplate.
The CellDisplayTemplate property 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.
For additional information about templates in the Grid component, refer to the following topic: Templates in Blazor Grid.
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">
<CellDisplayTemplate Context="displayContext">
@{
string value;
if (displayContext.Grid.IsEditingRow(displayContext.VisibleIndex)) {
var editModel = (Employee)displayContext.Grid.GetEditContext().Model;
value = editModel.FirstName;
}
else
value = displayContext.DisplayText;
}
<span>@value</span>
</CellDisplayTemplate>
</DxGridDataColumn>
<DxGridDataColumn FieldName="LastName" />
<DxGridDataColumn FieldName="BirthDate" />
<DxGridDataColumn FieldName="HireDate" />
</Columns>
</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();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Grid.Northwind {
public partial class Employee {
public int EmployeeId { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string Title { get; set; }
public string TitleOfCourtesy { get; set; }
public Nullable BirthDate { get; set; }
[Required]
[Range(typeof(DateTime), "1/1/2000", "1/1/2020",
ErrorMessage = "HireDate must be between {1:d} and {2:d}")]
public Nullable HireDate { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string HomePhone { get; set; }
public string Extension { get; set; }
public byte[] Photo { get; set; }
public string Notes { get; set; }
public Nullable<int> ReportsTo { get; set; }
public string PhotoPath { get; set; }
public virtual ICollection<Order> Orders { get; set; }
public string Text => $"{FirstName} {LastName} ({Title})";
public string ImageFileName => $"Employees/{EmployeeId}.jpg";
}
}
using Microsoft.EntityFrameworkCore;
#nullable disable
namespace Grid.Northwind {
public partial class NorthwindContext : DbContext {
public NorthwindContext(DbContextOptions<NorthwindContext> options)
: base(options) {
}
// ...
public virtual DbSet<Employee> Employees { get; set; }
// ...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
if(!optionsBuilder.IsConfigured) {
optionsBuilder.UseSqlServer("Server=.\\sqlexpress;Database=Northwind;Integrated Security=true");
}
}
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder) {
// ...
modelBuilder.Entity<Employee>(entity => {
entity.HasIndex(e => e.EmployeeId, "EmployeeId");
entity.HasIndex(e => e.LastName, "LastName");
entity.HasIndex(e => e.FirstName, "FirstName");
entity.HasIndex(e => e.Title, "Title");
entity.HasIndex(e => e.BirthDate, "BirthDate");
entity.HasIndex(e => e.HireDate, "HireDate");
entity.HasIndex(e => e.Notes, "Notes");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
using Microsoft.EntityFrameworkCore;
// ...
builder.Services.AddDbContextFactory<NorthwindContext>((sp, options) => {
var env = sp.GetRequiredService<IWebHostEnvironment>();
var dbPath = Path.Combine(env.ContentRootPath, "Northwind-5e44b51f.mdf");
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;AttachDbFileName=" + dbPath);
});
The following example displays the More Info… links in cells of a column bound to the EmployeeId field. Users can click these links to view details about data rows in a pop-up window.
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable
<DxGrid Data="@Data">
<Columns>
<DxGridDataColumn FieldName="FirstName" />
<DxGridDataColumn FieldName="LastName" />
<DxGridDataColumn FieldName="Title" />
<DxGridDataColumn FieldName="BirthDate" />
<DxGridDataColumn FieldName="HireDate" />
<DxGridDataColumn FieldName="EmployeeId" Caption="Details" AllowSort="false">
<CellDisplayTemplate>
<a class="d-block text-left" href="javascript:;" @onclick="() => ShowDetails(context)">More Info...</a>
</CellDisplayTemplate>
</DxGridDataColumn>
</Columns>
</DxGrid>
<DxPopup @bind-Visible="@PopupVisible"
HeaderText="@PopupHeaderText"
HorizontalAlignment="HorizontalAlignment.Center"
VerticalAlignment="VerticalAlignment.Center">
@PopupContent
</DxPopup>
@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();
}
bool PopupVisible {
get { return CurrentEmployee != null; }
set { if(!value) CurrentEmployee = null; }
}
string PopupHeaderText {
get { return CurrentEmployee != null ? CurrentEmployee.FirstName + " " + CurrentEmployee.LastName : ""; }
}
string PopupContent {
get { return CurrentEmployee != null ? CurrentEmployee.Notes : ""; }
}
public void ShowDetails(GridDataColumnCellDisplayTemplateContext context) {
CurrentEmployee = Data.Where(e => e.EmployeeId == (int)context.Value).FirstOrDefault();
}
public void Dispose() {
Northwind?.Dispose();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Grid.Northwind {
public partial class Employee {
public int EmployeeId { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string Title { get; set; }
public string TitleOfCourtesy { get; set; }
public Nullable BirthDate { get; set; }
[Required]
[Range(typeof(DateTime), "1/1/2000", "1/1/2020",
ErrorMessage = "HireDate must be between {1:d} and {2:d}")]
public Nullable HireDate { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string HomePhone { get; set; }
public string Extension { get; set; }
public byte[] Photo { get; set; }
public string Notes { get; set; }
public Nullable<int> ReportsTo { get; set; }
public string PhotoPath { get; set; }
public virtual ICollection<Order> Orders { get; set; }
public string Text => $"{FirstName} {LastName} ({Title})";
public string ImageFileName => $"Employees/{EmployeeId}.jpg";
}
}
using Microsoft.EntityFrameworkCore;
#nullable disable
namespace Grid.Northwind {
public partial class NorthwindContext : DbContext {
public NorthwindContext(DbContextOptions<NorthwindContext> options)
: base(options) {
}
// ...
public virtual DbSet<Employee> Employees { get; set; }
// ...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
if(!optionsBuilder.IsConfigured) {
optionsBuilder.UseSqlServer("Server=.\\sqlexpress;Database=Northwind;Integrated Security=true");
}
}
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder) {
// ...
modelBuilder.Entity<Employee>(entity => {
entity.HasIndex(e => e.EmployeeId, "EmployeeId");
entity.HasIndex(e => e.LastName, "LastName");
entity.HasIndex(e => e.FirstName, "FirstName");
entity.HasIndex(e => e.Title, "Title");
entity.HasIndex(e => e.BirthDate, "BirthDate");
entity.HasIndex(e => e.HireDate, "HireDate");
entity.HasIndex(e => e.Notes, "Notes");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
using Microsoft.EntityFrameworkCore;
// ...
builder.Services.AddDbContextFactory<NorthwindContext>((sp, options) => {
var env = sp.GetRequiredService<IWebHostEnvironment>();
var dbPath = Path.Combine(env.ContentRootPath, "Northwind-5e44b51f.mdf");
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;AttachDbFileName=" + dbPath);
});
Run Demo: Grid - Column Templates
The following example creates a template grid column that shows the Show Details link. Users can click this link to view detail information about the current grid record. The information is displayed in the Blazor Form Layout.
View Example: Grid - Show Detail Information in DxFormLayout
See Also