5 Dec 202516 minutes to read
This section briefly explains about how to includeSyncfusion® Blazor DataGrid in your Blazor webAssembly app using Visual Studio, Visual Studio code and .NET CLI.
Ready to streamline your Syncfusion® Blazor development?
Discover the full potential of Syncfusion® Blazor components with Syncfusion® AI Coding Assistants. Effortlessly integrate, configure, and enhance your projects with intelligent, context-aware code suggestions, streamlined setups, and real-time insights—all seamlessly integrated into your preferred AI-powered IDEs like VS Code, Cursor, Syncfusion® CodeStudio and more.Explore Syncfusion® AI Coding Assistants
ABlazor WebAssembly App can be created usingVisual Studio 2022 with the built-inMicrosoft templates or theSyncfusion® Blazor Extension.
NOTE
For detailed steps, refer toMicrosoft Blazor tooling documentation.
To integrate the Blazor DataGrid component, install the required NuGet packages in theBlazor WebAssembly project:
OpenNuGet Package Manager in Visual Studio:
Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
Search and install the following packages:
Alternatively, use thePackage Manager Console:
Install-PackageSyncfusion.Blazor.Grid-Version32.1.19Install-PackageSyncfusion.Blazor.Themes-Version32.1.19NOTE
Syncfusion® Blazor components are available onnuget.org. For a complete list of packages, refer toNuGet packages.
ABlazor WebAssembly App can be created usingVisual Studio Code with the built-inMicrosoft templates or theSyncfusion® Blazor Extension.
dotnetnewblazorwasm-oBlazorAppcdBlazorAppTo integrate the Blazor DataGrid component, install the required Syncfusion® NuGet packages using theintegrated terminal:
dotnetaddpackageSyncfusion.Blazor.Grid-v32.1.19dotnetaddpackageSyncfusion.Blazor.Themes-v32.1.19dotnetrestoreNOTE
Syncfusion® Blazor components are available onnuget.org. For a complete list of packages, refer toNuGet packages.
Latest version of the.NET Core SDK. If you previously installed the SDK, you can determine the installed version by executing the following command in a command prompt (Windows) or terminal (macOS) or command shell (Linux).
dotnet--versiondotnetnewblazorwasm-oBlazorAppcdBlazorAppFor additional details, refer to:
To integrate the Blazor DataGrid component in aBlazor WebAssembly App using the.NET CLI:
Run the following commands to install the required NuGet packages:
dotnetaddpackageSyncfusion.Blazor.Grid-Version32.1.19dotnetaddpackageSyncfusion.Blazor.Themes-Version32.1.19dotnetrestoreNOTE
For more details, refer to:
- Install and manage packages using the dotnet CLI
- Syncfusion® Blazor components are available onnuget.org. For a complete list of packages, refer toNuGet packages.
@using Syncfusion.Blazor@using Syncfusion.Blazor.GridsOpen the~/Program.cs file and register the Syncfusion® Blazor service by adding the following code:
usingMicrosoft.AspNetCore.Components.Web;usingMicrosoft.AspNetCore.Components.WebAssembly.Hosting;usingSyncfusion.Blazor;varbuilder=WebAssemblyHostBuilder.CreateDefault(args);builder.RootComponents.Add("#app");builder.RootComponents.Add("head::after");builder.Services.AddScoped(sp=>newHttpClient{BaseAddress=newUri(builder.HostEnvironment.BaseAddress)});builder.Services.AddSyncfusionBlazor();awaitbuilder.Build().RunAsync();The theme stylesheet and script files are provided throughStatic Web Assets in the NuGet packages. Include these references in the<head> section of the~/wwwroot/index.html file:
<head> ....<linkhref="_content/Syncfusion.Blazor.Themes/bootstrap5.css"rel="stylesheet"/><scriptsrc="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js"type="text/javascript"></script></head>NOTE
- Refer toBlazor Themes for various methods to reference themes in a Blazor application:
>* [Static Web Assets](https://blazor.syncfusion.com/documentation/appearance/themes#static-web-assets)>* [CDN](https://blazor.syncfusion.com/documentation/appearance/themes#cdn-reference)>* [Custom Resource Generator (CRG)](https://blazor.syncfusion.com/documentation/common/custom-resource-generator)
- For script reference options, seeAdding Script References.
Add the Syncfusion® Blazor DataGrid component in the~/Pages/Home.razor file.
<SfGrid></SfGrid>The DataGrid requires a data source to display records. A collection implementing **IEnumerable
Data binding is typically performed in theOnInitialized lifecycle method of the component.
<SfGridDataSource="@Orders"></SfGrid>@code { public List<Order> Orders { get; set; } protected override void OnInitialized() { Orders = Enumerable.Range(1, 75).Select(x => new Order() { OrderID = 1000 + x, CustomerID = (new[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)], Freight = 2.1 * x, OrderDate = DateTime.Now.AddDays(-x) }).ToList(); } public class Order { public int? OrderID { get; set; } public string CustomerID { get; set; } public DateTime? OrderDate { get; set; } public double? Freight { get; set; } }}The DataGrid automatically generates columns when no explicit column definitions are provided. For greater control over column behavior and appearance, use theGridColumns component along with individualGridColumn elements to define columns explicitly.
Common Column Properties
<SfGridDataSource="@Orders"><GridColumns><GridColumnField=@nameof(Order.OrderID)HeaderText="Order ID"TextAlign="TextAlign.Right"Width="120"></GridColumn><GridColumnField=@nameof(Order.CustomerID)HeaderText="Customer Name"Width="150"></GridColumn><GridColumnField=@nameof(Order.OrderDate)HeaderText="Order Date"Format="d"Type="ColumnType.Date"TextAlign="TextAlign.Right"Width="130"></GridColumn><GridColumnField=@nameof(Order.Freight)HeaderText="Freight"Format="C2"TextAlign="TextAlign.Right"Width="120"></GridColumn></GridColumns></SfGrid>Paging allows the DataGrid to display records in a paged view, improving performance and readability for large datasets. Enable paging by setting theAllowPaging property totrue. Paging behavior can be customized using theGridPageSettings component.
<SfGridDataSource="@Orders"AllowPaging="true"><GridPageSettingsPageSize="5"></GridPageSettings><GridColumns><GridColumnField=@nameof(Order.OrderID)HeaderText="Order ID"TextAlign="TextAlign.Right"Width="120"></GridColumn><GridColumnField=@nameof(Order.CustomerID)HeaderText="Customer Name"Width="150"></GridColumn><GridColumnField=@nameof(Order.OrderDate)HeaderText="Order Date"Format="d"Type="ColumnType.Date"TextAlign="TextAlign.Right"Width="130"></GridColumn><GridColumnField=@nameof(Order.Freight)HeaderText="Freight"Format="C2"TextAlign="TextAlign.Right"Width="120"></GridColumn></GridColumns></SfGrid>Sorting allows the DataGrid to arrange records based on column values. Enable this feature by setting theAllowSorting property totrue. Sorting behavior can be customized using theGridSortSettings component.
<SfGridDataSource="@Orders"AllowPaging="true"AllowSorting="true"><GridPageSettingsPageSize="5"></GridPageSettings><GridColumns><GridColumnField=@nameof(Order.OrderID)HeaderText="Order ID"TextAlign="TextAlign.Right"Width="120"></GridColumn><GridColumnField=@nameof(Order.CustomerID)HeaderText="Customer Name"Width="150"></GridColumn><GridColumnField=@nameof(Order.OrderDate)HeaderText=" Order Date"Format="d"Type="ColumnType.Date"TextAlign="TextAlign.Right"Width="130"></GridColumn><GridColumnField=@nameof(Order.Freight)HeaderText="Freight"Format="C2"TextAlign="TextAlign.Right"Width="120"></GridColumn></GridColumns></SfGrid>Filtering allows the DataGrid to display a subset of records based on specified criteria. Enable filtering by setting theAllowFiltering property totrue. Filtering behavior can be customized using theGridFilterSettings component.
<SfGridDataSource="@Orders"AllowPaging="true"AllowSorting="true"AllowFiltering="true"><GridPageSettingsPageSize="5"></GridPageSettings><GridColumns><GridColumnField=@nameof(Order.OrderID)HeaderText="Order ID"TextAlign="TextAlign.Right"Width="120"></GridColumn><GridColumnField=@nameof(Order.CustomerID)HeaderText="Customer Name"Width="150"></GridColumn><GridColumnField=@nameof(Order.OrderDate)HeaderText=" Order Date"Format="d"Type="ColumnType.Date"TextAlign="TextAlign.Right"Width="130"></GridColumn><GridColumnField=@nameof(Order.Freight)HeaderText="Freight"Format="C2"TextAlign="TextAlign.Right"Width="120"></GridColumn></GridColumns></SfGrid>Grouping organizes records into logical groups based on column values. Enable grouping by setting theAllowGrouping property totrue. Grouping behavior can be customized using theGridGroupSettings component.
<SfGridDataSource="@Orders"AllowPaging="true"AllowSorting="true"AllowFiltering="true"AllowGrouping="true"><GridPageSettingsPageSize="5"></GridPageSettings><GridColumns><GridColumnField=@nameof(Order.OrderID)HeaderText="Order ID"TextAlign="TextAlign.Right"Width="120"></GridColumn><GridColumnField=@nameof(Order.CustomerID)HeaderText="Customer Name"Width="150"></GridColumn><GridColumnField=@nameof(Order.OrderDate)HeaderText="Order Date"Format="d"Type="ColumnType.Date"TextAlign="TextAlign.Right"Width="130"></GridColumn><GridColumnField=@nameof(Order.Freight)HeaderText="Freight"Format="C2"TextAlign="TextAlign.Right"Width="120"></GridColumn></GridColumns></SfGrid>
Exceptions that occur during DataGrid operations can be captured without interrupting the application flow. Use theOnActionFailure event to retrieve error details and handle them gracefully.
Key Points:
TValue on bothSfGrid andGridEvents for proper event argument binding.NOTE
Binding the
OnActionFailureevent during development helps identify issues early. Exception details can be logged or displayed for troubleshooting.
@using Syncfusion.Blazor.Data<spanclass="error">@ErrorDetails</span><SfGridTValue="Order"AllowPaging="true"><GridEventsTValue="Order"OnActionFailure="@ActionFailure"></GridEvents><GridPageSettingsPageSize="10"></GridPageSettings><SfDataManagerUrl="https://some.com/invalidUrl"Adaptor="Adaptors.WebApiAdaptor"></SfDataManager><GridColumns><GridColumnField=@nameof(Order.OrderID)HeaderText="Order ID"IsPrimaryKey="true"TextAlign="TextAlign.Right"Width="120"></GridColumn><GridColumnField=@nameof(Order.CustomerID)HeaderText="Customer Name"Width="150"></GridColumn><GridColumnField=@nameof(Order.OrderDate)HeaderText="Order Date"Format="d"Type="ColumnType.Date"TextAlign="TextAlign.Right"Width="130"></GridColumn><GridColumnField=@nameof(Order.Freight)HeaderText="Freight"Format="C2"TextAlign="TextAlign.Right"Width="120"></GridColumn></GridColumns></SfGrid><style> .error { color: red; }</style>@code { public string ErrorDetails = ""; public class Order { public int? OrderID { get; set; } public string CustomerID { get; set; } public DateTime? OrderDate { get; set; } public double? Freight { get; set; } } public void ActionFailure(FailureEventArgs args) { ErrorDetails = "Server exception: 404 Not Found"; StateHasChanged(); }}