This repository demonstrates a production-ready pattern for binding a PostgreSQL database to the Syncfusion Blazor Pivot Table. The sample reads order records stored in a PostgreSQL table and loads them into the Pivot Table so they can be aggregated and analyzed within the Blazor UI.
The implementation focuses on a clean data-access flow where the Pivot Table works against data loaded from PostgreSQL through the sample application, rather than requiring direct database access from the client.
- PostgreSQL Integration: Order records are persisted in a PostgreSQL table (
public.orders) and read through the Npgsql ADO.NET provider. - Syncfusion Blazor Pivot Table: Field list, drill-through, and configurable rows, columns, and values for multidimensional analysis.
- Data Binding Support: The sample uses Syncfusion's Blazor data-binding capabilities to connect the Pivot Table to the project data flow.
- Interactive Pivot Analysis: Explore aggregated values and inspect underlying records from the drill-through experience.
- Primary Key Configuration: The
BeginDrillThroughevent marksOrderIDas the primary key column so drill-through operations target the correct record. - Configurable Development Port: Server port managed via
Properties/launchSettings.json.
Sample Location / Source Code: https://github.com/SyncfusionExamples/syncfusion-blazor-pivot-table-postgresql-database-binding-sample
| Component | Version | Purpose |
|---|---|---|
| Visual Studio 2022 / VS Code | Latest | Development IDE with Blazor workload |
| .NET SDK | net10.0 or compatible | Runtime and build tools |
| PostgreSQL | 13 or later | Relational database server |
| Npgsql NuGet package | Latest | PostgreSQL ADO.NET data provider |
| Syncfusion.Blazor.PivotTable | Latest | Pivot Table UI component |
| Syncfusion.Blazor.Themes | Latest | Styling for the Pivot Table component |
| Syncfusion.Blazor.Data | Latest | Data adaptors including URL Adaptor support |
| Newtonsoft.Json | Latest | JSON serialization for CRUD models |
Note: Install the latest version of the required Syncfusion Blazor NuGet packages and the latest available version of Npgsql from NuGet. Do not pin to a specific patch version.
Ensure the PostgreSQL service is running on your machine. The sample expects the server to be reachable at:
Host=localhost
Port=5432
Connect to PostgreSQL using psql, pgAdmin, or any SQL client and create the database:
CREATE DATABASE "OrderDB";Switch to the OrderDB database and create the orders table used by the sample:
CREATE TABLE public.orders
(
orderid SERIAL PRIMARY KEY,
customername VARCHAR(100),
employeeid INTEGER,
shipcity VARCHAR(100),
freight NUMERIC(10, 2)
);Seed the table with a few rows so the Pivot Table has data to render on first load:
INSERT INTO public.orders (customername, employeeid, shipcity, freight) VALUES
('Toms', 1, 'New York', 35.30),
('Ravi', 2, 'London', 80.20),
('Sven', 1, 'Berlin', 52.10),
('Sara', 3, 'Madrid', 18.40),
('Paul', 2, 'Tokyo', 64.75);The connection string is defined in Controllers/OrderController.cs:
string ConnectionString = @"Host=localhost;Port=5432;Database=yourDB;Username=postgres;Password=password@123;";Update the following values to match your local PostgreSQL installation:
| Property | Default | Description |
|---|---|---|
Host |
localhost |
PostgreSQL server host |
Port |
5432 |
PostgreSQL server port |
Database |
yourDB |
Target database name |
Username |
postgres |
Database user with read/write privileges |
Password |
password@123 |
Password for the database user |
| File/Folder | Purpose |
|---|---|
/Models |
Contains the Order data model representing an order record |
/Data |
Data access logic for interacting with PostgreSQL |
/Components/Pages/Home.razor |
Pivot Table page with data binding, cell editing, and drill-through setup |
/Components/App.razor |
Root component that references Syncfusion stylesheet and syncfusion-blazor.min.js |
/Program.cs |
Service registration for ASP.NET Core and Syncfusion Blazor |
/Properties/launchSettings.json |
Port configuration for the local development server |
/URLAdaptor.csproj |
Project file with Syncfusion Blazor packages and Npgsql NuGet references |
The sample follows a straightforward data access flow:
PostgreSQL Database (public.orders table)
↓
Sample data access logic
↓
Syncfusion Blazor Pivot Table
- The sample reads the order rows from the PostgreSQL
orderstable. - The resulting data is loaded into the Pivot Table for aggregation and analysis.
- The Pivot Table renders the grouped results based on the configured fields.
Create the OrderDB database and the orders table as described in the PostgreSQL Setup section, then verify connectivity with psql or pgAdmin.
Update the ConnectionString field at the top of OrderController.cs to match your PostgreSQL credentials:
string ConnectionString = @"Host=localhost;Port=5432;Database=OrderDB;Username=postgres;Password=<your-password>;";Program.cs registers the services required by the sample:
builder.Services.AddSyncfusionBlazor();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();The project uses the standard Blazor service registration pattern for the Pivot Table and its supporting components.
-
Clone the repository
git clone https://github.com/SyncfusionExamples/syncfusion-blazor-pivot-table-postgresql-database-binding-sample cd "syncfusion-blazor-pivot-table-postgresql-database-binding-sample" cd "URLAdaptor"
Repository: https://github.com/SyncfusionExamples/syncfusion-blazor-pivot-table-postgresql-database-binding-sample
-
Restore packages and build
From the repository root, change into the Blazor project folder and restore the dependencies:
cd URLAdaptor dotnet restore dotnet build -
Start PostgreSQL and seed the database
Ensure PostgreSQL is running and that the
OrderDBdatabase andorderstable exist (see PostgreSQL Setup). -
Run the application
From inside the
URLAdaptorproject folder, start the application withdotnet run:cd URLAdaptor dotnet run -
Open the application
Navigate to the local URL displayed in the terminal (typically
https://localhost:7169orhttp://localhost:5145).
The sample is intended for interactive pivot analysis, including drill-through and editing experiences handled by the Syncfusion Blazor Pivot Table and its data-binding setup.
- Double-click an aggregated value cell in the pivot view.
- A drill-through grid opens showing the underlying PostgreSQL records.
- The
BeginDrillThroughevent handler marks theOrderIDcolumn asIsPrimaryKey = trueso that operations from the drill-through grid target the correct record. - Close the drill-through window to return to the pivot view.
The Order model represents a single row in the PostgreSQL orders table:
public class Order
{
[Key]
public int? OrderID { get; set; }
public string? CustomerName { get; set; }
public int? EmployeeID { get; set; }
public decimal? Freight { get; set; }
public string? ShipCity { get; set; }
}| Field | Type | Pivot Role | Description |
|---|---|---|---|
OrderID |
int |
Primary Key | Unique identifier for the order |
CustomerName |
string |
Row | Customer name used as a pivot row field |
EmployeeID |
int |
Column | Employee ID used as a pivot column field |
Freight |
decimal |
Value | Freight amount used as the pivot value (aggregated measure) |
ShipCity |
string |
Dimension | Ship city used as an additional pivot field |
Components/App.razor references the Syncfusion stylesheet and script bundle:
<link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>If CSS or scripts fail to load, check the browser developer tools (F12) for 404 errors on _content/Syncfusion.* paths.
- Verify
Host,Port,Database,Username, andPasswordinOrderController.csmatch your PostgreSQL installation. - Confirm the PostgreSQL service is running and reachable on
localhost:5432:- Windows:
Get-Service postgresql*andStart-Service postgresql-x64-<version> - macOS (Homebrew):
brew services start postgresql - Linux:
sudo systemctl start postgresql
- Windows:
- Test the connection from
psqlusing the same credentials:psql -h localhost -p 5432 -U postgres -d OrderDB
- Ensure the database user has read/write privileges on the
public.orderstable.
- Confirm the development port in
Properties/launchSettings.jsonis available. - If needed, change the port numbers in
launchSettings.jsonand restart the application.
For step-by-step guidance on integrating PostgreSQL with the Syncfusion Blazor Pivot Table, including UrlAdaptor configuration, CRUD operations, and database connectivity, see the official PostgreSQL database binding documentation.