-
Notifications
You must be signed in to change notification settings - Fork 73
1004091: Need to create documentation for integrating SQL with DataGrid #7440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
|
Build Status: INPROGRESS 🔃 |
|
CI Status: FAILURE ❌ |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: FAILURE ❌ |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: FAILURE ❌ |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: FAILURE ❌ |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: FAILURE ❌ |
|
Build Status: INQUEUE 🕒 |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: FAILURE ❌ |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: SUCCESS ✅ |
…SfGrid rendering after importing namespace
|
Build Status: INPROGRESS 🔃 |
|
CI Status: SUCCESS ✅ |
|
Build Status: INPROGRESS 🔃 |
|
CI Status: SUCCESS ✅ |
layout: post
title: Microsoft SQL Server Data Binding in Blazor DataGrid | Syncfusion
description: Learn how to bind Microsoft SQL Server data to Syncfusion Blazor DataGrid with server-side searching, filtering, sorting, paging, grouping and CRUD operations.
platform: Blazor
control: DataGrid
documentation: ug
Connecting Microsoft SQL Server Data to Blazor DataGrid
Syncfusion® Blazor DataGrid seamlessly integrates with Microsoft SQL Server, enabling direct data binding, server-side operations (searching, filtering, sorting, paging, grouping), and full CRUD functionality using raw SQL queries.
Microsoft SQL Server offers enterprise-grade capabilities for building scalable, secure data-driven applications with Blazor. Integrating SQL Server directly with Syncfusion DataGrid provides significant advantages:
Prerequisites
Ensure the following software and packages are installed before proceeding:
SQL setup environment
Step 1: Create database and table in SQL Server
The Network Support Ticket System scenario demonstrates binding the Syncfusion Blazor DataGrid to a Microsoft SQL Server database using real ticket records and server‑side operations. The "NetworkSupportDB" database and its "Tickets" table store the ticket data required for performing CRUD operations, enabling grid-driven querying, editing, paging, and aggregation.
Open SQL Server Management Studio (SSMS) and execute the following script to create the database and "Tickets" table:
With the database and Tickets table provisioned, continue by installing the NuGet packages required for Syncfusion Blazor components and SQL Server connectivity.
These dependencies will be referenced by the grid adaptor and data access layer in the following steps.
Step 2: Install NuGet packages
Syncfusion.Blazor provides the DataGrid and related UI services, while Microsoft.Data.SqlClient enables secure, efficient connectivity to SQL Server with raw query support.
Open Visual Studio, navigate to the Package Manager Console, and install the necessary package:
Alternatively, use NuGet Package Manager UI:
Package installation has been completed. Continue with defining the data model aligned with the Tickets schema to ensure strong typing and reliable DataGrid binding.
Step 3: Define the data model
A strongly typed model establishes a clear contract between the database schema and the grid, enabling compile-time validation and predictable serialization. Aligning properties with SQL column data types ensures accurate binding, editing, and server-side processing.
Create a new folder Data, inside the folder create a new file Tickets.cs to design a model class representing the Tickets table structure:
Model definition has been completed. Proceed to configure the connection string to enable database connectivity.
Step 4: Configure data access (TicketData.cs)
Define the SQL Server connection string and implement the data retrieval method by creating a new folder Data and inside the folder create a new file TicketData.cs to establish reliable connectivity and server-side data access. The implementation uses "SqlConnection", "SqlCommand", and "SqlDataAdapter" to execute a SELECT statement and materialize results into the strongly typed "Tickets" model.
Connection string parameters:
Key implementation details:
Data access configuration has been completed. Proceed to register Syncfusion Blazor services.
Integrate Syncfusion Blazor DataGrid
Step 1: Install NuGet packages and register Syncfusion services
Open Visual Studio, navigate to the Package Manager Console, and install the necessary package:
Registers the Syncfusion component services required by the DataGrid at runtime and exposes grid APIs via dependency injection.
Configure Syncfusion services in Program.cs:
Import Namespaces
Open the _Imports.razor file and add required namespaces.
Add stylesheet and script resources
Add Syncfusion styles and scripts in Components/App.razor. The theme stylesheet and script files are provided through Static Web Assets in the NuGet packages.
Add Syncfusion Blazor DataGrid component
Create new Home.razor file and add the Syncfusion® Blazor DataGrid component. The DataGrid automatically generates columns when no explicit column definitions are provided. For greater control over column behavior and appearance, use GridColumn to specify each column and configure properties such as Field, HeaderText and Width. This approach provides full control over column behavior and appearance.
Service registration and namespace imports have been completed. Continue with configuring DataGrid–SQL Server integration using the Custom Adaptor.
Step 2: Bind data from Microsoft SQL Server using Custom Adaptor
The Syncfusion® Blazor DataGrid can bind data from a Microsoft SQL Server database using DataManager and set the Adaptor property to Custom Adaptor for scenarios that require full control over data operations.
The Custom Adaptor serves as the bridge between DataGrid UI interactions and SQL Server database operations. When users interact with the grid (search, filter, sort, page), the adaptor intercepts these requests and executes corresponding SQL operations.
Implement custom adaptor
Create a Custom Adaptor class in Components/Pages/Home.razor that bridges DataGrid actions with SQL Server operations:
Custom Adaptor methods reference:
Bind the created adaptor to the DataGrid markup in Home.razor.
Data flow architecture:
The Custom Adaptor implementation centralizes all database logic, enabling consistent SQL execution, error handling, and performance optimization across all grid operations.
Perform data operations
Server-side data operations optimize performance by processing data before transmission to the client. Each operation in the Custom Adaptor's
ReadAsyncmethod handles specific grid functionality. The Syncfusion® Blazor DataGrid sends operation details to the API through a DataManagerRequest object. These details can be applied to the data source using methods from the DataOperations class.Common Methods in DataOperations
Searching
Enables keyword-based query across configured fields, allowing the grid to delegate search criteria to the adaptor for efficient server-side filtering. The built-in
PerformSearchingmethod of theDataOperationsclass applies search criteria from theDataManagerRequestto the data source.To enable searching add the Search item to the toolbar using the Toolbar property in the DataGrid markup.
Filtering
Provides column-level criteria evaluation so the adaptor can restrict datasets at the source for precise, efficient retrieval. The built-in
PerformFilteringmethod in theDataOperationsclass applies filter criteria from theDataManagerRequestto the data collection.To enable filtering in the DataGrid markup, set the AllowFiltering property to "true". Configure filtering behavior and appearance using the GridFilterSettings property.
Sorting
Enables deterministic record ordering by delegating sort descriptors to the adaptor for database-optimized sorting. The built-in
PerformSortingmethod in theDataOperationsclass applies sort criteria from theDataManagerRequestto the data collection.To enable sorting in the DataGrid markup, set the AllowSorting property to "true". Configure initial sorting by setting the Field (the column's data field name) and Direction properties in the Columns collection of GridSortSettings.
Aggregation
Aggregate functions compute summary statistics across datasets without requiring row-level retrieval, enabling efficient calculation of totals, averages, and counts at the database server level. The built-in
PerformAggregationmethod in the DataUtil class calculates aggregate values based on the criteria specified in theDataManagerRequest.Enable aggregates by including GridAggregateColumn component in DataGrid markup, for each aggregate column, specify at least the Field and Type properties.
Paging
Paging divides large result sets into fixed-size pages, reducing memory consumption and improving client-side responsiveness by retrieving only the requested page from the server.The built-in
PerformSkip,PerformTakemethods in theDataOperationsclass apply paging criteria from theDataManagerRequestto the data collection.To enable paging in the DataGrid markup, set the AllowPaging property to "true". Paging options can be configured through the GridPageSettings component. GridPageSettings allows control of page size, current page, and total record count.
Grouping
Grouping hierarchically organizes records by specified column values, enabling data summarization and nested record visualization while reducing query complexity through server-side grouping operations.The built-in
Groupmethod in theDataUtilclass applies grouping logic based on the configuration in theDataManagerRequest.To enable grouping in the DataGrid markup, set the AllowGrouping property to "true". Configure grouping behavior using GridGroupSettings.
Perform CRUD operations
Custom Adaptor methods enable users to create, read, update, and delete records directly from the DataGrid. Each operation calls corresponding data layer methods in TicketData.cs to execute SQL commands.
Create (Insert) records
Record insertion enables users to create new tickets directly from the DataGrid interface; the adaptor intercepts the insertion request, applies business logic validation, and persists the new record to the SQL Server database.
In Home.razor implement
InsertAsyncto handle new record creation:In Data/TicketData.cs, implement the insert method:
Now the new ticket is persisted to the database and reflected in the grid.
Read (Retrieve) records
Record retrieval executes SQL queries against the database and materializes results into the strongly typed Tickets model; the adaptor processes data operations (search, filter, sort, aggregate, page, group) before returning the result set to the grid for display.
The
ReadAsyncmethod is implemented in Step 02, retrieving and processing records from SQL Server which returns either the IEnumerable result or aDataResultobject withResultandCount.Now the grid displays SQL Server data with full support for searching, filtering, sorting, paging, grouping, and aggregation.
Update (Edit) records
Record modification enables users to edit ticket attributes directly within the DataGrid; the adaptor captures the edited row, validates changes, and synchronizes modifications to the SQL Server database while maintaining data integrity.
Implement
UpdateAsyncto handle record modifications:In Data/TicketData.cs, implement the update method:
Now the modifications are synchronized to the database and reflected in the grid UI.
Delete (Remove) records
Record deletion enables users to remove tickets directly from the DataGrid; the adaptor intercepts the deletion request, executes corresponding SQL DELETE statements, and removes the record from both the database and grid display.
In Home.razor implement
RemoveAsyncto handle record deletion:Now the tickets are removed from the database and the grid UI reflects the changes immediately.
In Data/TicketData.cs, implement the delete method:
Batch operations
Batch operations aggregate multiple insertions, updates, and deletions into a single request, reducing network overhead and enabling transactional consistency by persisting all changes atomically to the SQL Server database.
In Home.razor implement
BatchUpdateAsyncto process multiple record changes in a single request:Now the adaptor supports bulk modifications with atomic database synchronization. All CRUD operations are now fully implemented, enabling comprehensive data management capabilities within the Blazor DataGrid.
Run the application
Build and launch the Blazor application: