Introduction
In this article, we will create a reusable grid component for Blazor called BlazorGrid. It will display the user data in a grid and will support the client-side pagination.
Take a look at the final application.

Since this is a reusable component, we will also publish it to nuget.org as a NuGet package. To learn how to publish the BlazorGrid component to a NuGet gallery, refer to my article Blazor - Publishing A Component To NuGet Gallery.
Prerequisites
- Install the .NET Core 2.1 or above SDK from here.
- Install the latest version of Visual Studio 2017 from here.
- Install ASP.NET Core Blazor Language Services extension from here.
- Install Blazor templates on the command line using the following command.
dotnet new -i Microsoft.AspNetCore.Blazor.Templates
Source Code: Get the source code from GitHub.
Creating the Blazor Library project
To create a Blazor library project, follow the steps mentioned below.
- Navigate to the folder where you want to create your project.
- Open command prompt or Windows PowerShell as administrator.
- Run the following command.
dotnet new blazorlib -o BlazorGridComponent
It will create a Blazor library project with the name BlazorGridComponent.
Open this project using Visual Studio. You will see a folder structure as shown in the image below.

There are some predefined files provided in this project. We will delete Component 1. cshtml, ExampleJsInterop.cs, exampleJsInterop.js, and background.png files to make our solution clean. Do not delete styles.css as we will put the CSS definitions for our custom component in this file.
Creating BlazorGrid component
Now, we will add the component view page to our project.
Right-click on the BlazorGridComponent project and then select Add >> New Item. An “Add New Item” dialog box will open; select “Web” from the left panel, then select “Razor View” from the templates panel, and name it BlazorGrid.cshtml. Click Add. Refer to the image below.

Open the BlazorGrid.cshtml file and put the following code into it.
@typeparam TableItem
<table>
<thead>
<tr class="jsgrid-grid-header">@GridHeader</tr>
</thead>
<tbody>
@foreach (var item in ItemList)
{
<tr class="jsgrid-row-item">@GridRow(item)</tr>
}
</tbody>
</table>
<div class="pagination">
<button class="btn page-button btn-info" onclick=@(async()=>SetPagerSize("back"))>«</button>
<button class="btn page-button btn-secondary" onclick=@(async()=>NavigateToPage("previous"))>Prev</button>
@for (int i = startPage; i <= endPage; i++)
{
var currentPage = i;
<button class="btn page-button @(currentPage == curPage ? "current-page" : "")" onclick=@(async()=>updateList(currentPage))>
@currentPage
</button>
}
<button class="btn page-button btn-secondary" onclick=@(async()=>NavigateToPage("next"))>Next</button>
<button class="btn page-button btn-info" onclick=@(async()=>SetPagerSize("forward"))>»</button>
<span class="page-button btn btn-link disabled">Page @curPage of @totalPages</span>
</div>
@functions{
int totalPages;
int curPage;
int pagerSize;
int startPage;
int endPage;
[Parameter]
RenderFragment GridHeader { get; set; }
[Parameter]
RenderFragment<TableItem> GridRow { get; set; }
[Parameter]
IEnumerable<TableItem> Items { get; set; }
[Parameter]
int PageSize { get; set; }
IEnumerable<TableItem> ItemList { get; set; }
protected override async Task OnInitAsync()
{
pagerSize = 5;
curPage = 1;
ItemList = Items.Skip((curPage - 1) * PageSize).Take(PageSize);
totalPages = (int)Math.Ceiling(Items.Count() / (decimal)PageSize);
SetPagerSize("forward");
}
public void updateList(int currentPage)
{
ItemList = Items.Skip((currentPage - 1) * PageSize).Take(PageSize);
curPage = currentPage;
this.StateHasChanged();
}
public void SetPagerSize(string direction)
{
if (direction == "forward" && endPage < totalPages)
{
startPage = endPage + 1;
if (endPage + pagerSize < totalPages)
{
endPage = startPage + pagerSize - 1;
}
else
{
endPage = totalPages;
}
this.StateHasChanged();
}
elseif (direction == "back" && startPage > 1)
{
endPage = startPage - 1;
startPage = startPage - pagerSize;
}
}
public void NavigateToPage(string direction)
{
if (direction == "next")
{
if (curPage < totalPages)
{
if (curPage == endPage)
{
SetPagerSize("forward");
}
curPage += 1;
}
}
elseif (direction == "previous")
{
if (curPage > 1)
{
if (curPage == startPage)
{
SetPagerSize("back");
}
curPage -= 1;
}
}
updateList(curPage);
}
}





DJ PRASATHPosted Dec 9, 2018, 4:07 AM
Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!!