Blazor Grid Component Overview
This article provides a quick introduction to get your first Blazor data grid component up and running in a few seconds. There is a video tutorial and a list of the key features.
The Telerik Blazor Data Grid provides a comprehensive set of ready-to-use features. They cover everything from paging, sorting, filtering, editing, and grouping to row virtualization, optimized data reading, keyboard navigation and accessibility support.
The Telerik Blazor grid is built on native Blazor from the ground up, by a company with a long history of making enterprise-ready Grids. This results in a highly customizable Grid that delivers lighting fast performance.
The Grid component is part of Telerik UI for Blazor, a
professional grade UI library with 110+ native components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.
Creating Blazor Grid
- Use the
TelerikGrid
tag. - Assign the Grid
Data
parameter to anIEnumerable<T>
property, or use theOnRead
event. We'll go withData
this time. The Grid Data Binding article compares the two alternatives. - (optional) Enable some data operations like paging, sorting or filtering.
- Add
GridColumn
instances under theGridColumns
tag. Each columnField
should point to the model property to display. Usenameof()
or the plain field name. Define user-friendly columnTitle
s orDisplayFormat
for numeric and date values.
@* Telerik Blazor Grid with some common features *@
<TelerikGrid Data="@GridData"
Pageable="true"
Sortable="true"
FilterMode="@GridFilterMode.FilterRow">
<GridColumns>
<GridColumn Field="Name" Title="Product Name" />
<GridColumn Field="Price" DisplayFormat="{0:C2}" />
<GridColumn Field="@nameof(Product.Released)" DisplayFormat="{0:D}" />
<GridColumn Field="@nameof(Product.Discontinued)" />
</GridColumns>
</TelerikGrid>
@code {
private List<Product> GridData { get; set; }
protected override void OnInitialized()
{
GridData = new List<Product>();
var rnd = new Random();
for (int i = 1; i <= 30; i++)
{
GridData.Add(new Product
{
Id = i,
Name = "Product name " + i,
Price = (decimal)(rnd.Next(1, 50) * 3.14),
Released = DateTime.Now.AddDays(-rnd.Next(1, 365)).AddYears(-rnd.Next(1, 10)).Date,
Discontinued = i % 5 == 0
});
}
base.OnInitialized();
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime Released { get; set; }
public bool Discontinued { get; set; }
}
}
Blazor Grid Video Tutorial
If you prefer video instructions, watch this short Blazor Grid video tutorial. It covers to following topics:
- Introduction to the Telerik Blazor Grid
- Add TelerikRootComponent to MainLayout
- Add a Blazor Grid
- Configure columns
- Enable additional features
Data Binding
The are two main ways to provide data to the Grid - via the Data
parameter and via the OnRead
event. Data
provides simplicity, while OnRead
allows more flexibility in complex scenarios, and performance when there is a lot of data.
Data Operations
The Blazor Grid supports all fundamental data operations out-of-the-box:
- Paging or alternatively, virtual scrolling
- Sorting
- Filtering
- Grouping. The Grid can also load the data for each group on demand.
- Aggregates
Editing
The Grid can perform CRUD operations on its current data - add, edit and delete rows. It exposes events that let you control the editing and commit changes to the actual data source.
The Grid offers several editing modes with different user experience - incell, inline and popup.
See Grid CRUD Operations Overview for more details.
Virtualization
The Blazor Grid features UI virtualization to improve browser performance:
Column Features
The Grid columns offer a rich set of functionality to enable immense flexibility for your application scenarios. The main column features include:
- Display Format for numeric and date values
- Resizing
- Reodering
- Column Menu to control data operations and column visibility
- Frozen columns, which do not scroll horizontally (also called locked columns)
- Multi-column Headers to group multiple column headers under a single parent header
- Column Events
- Visibility and Width
Templates
The Grid supports custom content in various parts of the component such as data cells, headers, footers, editors and more. See Grid Templates.
More Blazor Grid Features
- Selection—select one or multiple rows through clicks or checkboxes or select one or multiple cells through clicks.
- State—get or set the Grid configuration programmatically.
- Toolbar—define user actions in a toolbar above the header cells.
- Hierarchy—nest Grids and visualize parent-child relations between data records.
- Drag and drop rows—move rows in a Grid or between different Grids.
- Loading animation—show a loading animation to improve user experience during long data operations.
- Scrolling—the Grid will show standard scrollbars automatically if the data does not fit the current component width and height.
Grid Parameters
The following table lists Grid parameters, which are not discussed elsewhere in the component documentation. Check the Grid API Reference for a full list of parameters, methods and events.
Parameter | Type and Default Value | Description |
---|---|---|
Class |
string |
Additional CSS class for the <div class="k-grid"> element. Use it to apply custom styles or override the theme. For example, change the Grid font size. |
Height |
string |
A height style in any supported CSS unit. |
Navigable |
bool |
Enables keyboard navigation. |
Width |
string |
A width style in any supported CSS unit. The Grid has no default width, but expands horizontally to fill its container. |
Blazor Grid Reference and Methods
The Grid has methods to execute actions such as:
- rebind to refresh the data
- export to Excel and other formats
- automatically resize columns to fit their content
- get or set the Grid configuration state
- get the dragged data item and its drop index from the destination Grid instance
To execute these methods, obtain reference to the Grid instance via @ref
.
<TelerikButton OnClick="@AutoFit">Autofit All Columns</TelerikButton>
<TelerikGrid @ref="TheGrid"
Data="@GridData"
Width="600px">
<GridColumns>
<GridColumn Field="@(nameof(GridModel.Id))" />
<GridColumn Field="@(nameof(GridModel.Text))" Width="300px" />
</GridColumns>
</TelerikGrid>
@code {
private TelerikGrid<GridModel> TheGrid { get; set; }
private void AutoFit()
{
TheGrid.AutoFitAllColumns();
}
private IEnumerable<GridModel> GridData = Enumerable.Range(1, 5)
.Select(x => new GridModel
{
Id = x,
Text = "some longer text here that will not fit on a single line and we would like to expand it " + x
});
public class GridModel
{
public int Id { get; set; }
public string Text { get; set; }
}
}