Bind to Data
- 5 minutes to read
This document describes how to bind the DevExpress Blazor Pivot Table to data in different scenarios.
Bind to Runtime Data
You can bind the Pivot Table to runtime data created during component initialization.
- Bind the Data property to a C# field/property.
- Populate this field/property with data in the OnInitializedAsync lifecycle method.
- Add fields to the component. For more information, refer to the following article: Data Presentation Basics - Fields.
@rendermode InteractiveServer
<DxPivotTable Data="SalesData">
<Fields>
<DxPivotTableField Field="@nameof(Sales.SaleInfo.Region)"
Area="@PivotTableArea.Row"
AreaIndex="0" />
<DxPivotTableField Field="@nameof(Sales.SaleInfo.Country)"
Area="@PivotTableArea.Row"
SortOrder="@PivotTableSortOrder.Descending"
AreaIndex="1" />
<DxPivotTableField Field="@nameof(Sales.SaleInfo.Date)"
GroupInterval="@PivotTableGroupInterval.DateYear"
Area="@PivotTableArea.Column"
AreaIndex="0"
Caption="Year" />
<DxPivotTableField Field="@nameof(Sales.SaleInfo.Date)"
GroupInterval="@PivotTableGroupInterval.DateQuarter"
Area="@PivotTableArea.Column"
AreaIndex="1"
Caption="Quarter" />
<DxPivotTableField Field="@nameof(Sales.SaleInfo.Amount)"
SortOrder="@PivotTableSortOrder.Ascending"
Area="@PivotTableArea.Data"
SummaryType="@PivotTableSummaryType.Sum" />
</Fields>
</DxPivotTable>
@code {
IEnumerable<Sales.SaleInfo> SalesData;
protected override async Task OnInitializedAsync() {
SalesData = await Sales.GetSalesAsync();
}
}
Bind to Data with Entity Framework Core
You can use Entity Framework Core in Blazor Server applications. Follow the steps below to bind the Pivot Table to data.
Fetch data from a database. See steps 1-6 in the following article:
Add a Pivot Table to your application and bind its Data property to the created data collection.
- Add fields to the component. For more information, refer to the following article: Data Presentation Basics - Fields.
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable
@using DevExpress.Blazor.PivotTable
<DxPivotTable Data="Data">
<Fields>
<DxPivotTableField Field="CategoryName"
Area="@PivotTableArea.Row" />
<DxPivotTableField Field="ProductName"
Area="@PivotTableArea.Row" />
<DxPivotTableField Field="OrderDate"
Area="@PivotTableArea.Column"
GroupInterval="@PivotTableGroupInterval.DateYear"
Caption="Year" />
<DxPivotTableField Field="OrderDate"
Area="@PivotTableArea.Column"
GroupInterval="@PivotTableGroupInterval.DateQuarter"
Caption="Quarter">
<ValueTemplate>
<span>@($"Q{context.Text}")</span>
</ValueTemplate>
</DxPivotTableField>
<DxPivotTableField Field="UnitPrice"
Area="@PivotTableArea.Data"
SummaryType="@PivotTableSummaryType.Sum" />
<DxPivotTableField Field="ShipCountry"
Area="@PivotTableArea.Filter" />
<DxPivotTableField Field="ShipCity"
Area="@PivotTableArea.Filter" />
</Fields>
</DxPivotTable>
@code {
NorthwindContext Northwind { get; set; }
IEnumerable<object> Data { get; set; }
protected override async Task OnInitializedAsync() {
Northwind = NorthwindContextFactory.CreateDbContext();
var products = await Northwind.Products.ToListAsync();
var categories = await Northwind.Categories.ToListAsync();
var orders = await Northwind.Orders.ToListAsync();
var orderDetails = await Northwind.OrderDetails.ToListAsync();
Data = (from c in categories
join p in products on c.CategoryId equals p.CategoryId
join od in orderDetails on p.ProductId equals od.ProductId
select new {
CategoryName = c.CategoryName,
ProductName = p.ProductName,
UnitPrice = p.UnitPrice,
OrderDate = od.Order.OrderDate,
ShipCountry = od.Order.ShipCountry,
ShipCity = od.Order.ShipCity
})
.ToList();
}
public void Dispose() {
Northwind?.Dispose();
}
}
Virtual Scrolling
If the Pivot Table displays a large number of data cells, enable virtual scrolling to improve performance. Set the VirtualScrollingEnabled property to true
. In this mode, the Pivot Table renders data on demand as a user scrolls through rows/columns.
<DxPivotTable Data="SalesData"
VirtualScrollingEnabled="true">
<Fields>
@*...*@
</Fields>
</DxPivotTable>