Introduction
We will be looking at how to add filter/search features into a blazor server grid. One can enhance the useability of grids by adding field(s) that can receive values to be used to filter the records in a grid view. This can reduce the number of pages needed to be designed in an application because a single page can be used to handle data entry, data view, and even reporting. In this article, only the creation of filters will be looked into.
Programming Language
C# and HTML will be used.
Prerequisite
Basic knowledge of blazor design patterns and page hierarchy is needed to understand this article.
Background information
We are using a Postgres SQL database, so the following nugget packages were installed
- Npgsql
- Npgsql.EntityFrameworkCore.PostgreSql(5.0.0)
In the code below my application is called "payarenauser", so take note of the reference in the various class files. You have to replace it with your own project name. To reference data in an application, you need to import the namespace of the data folder, in my case, it is using payarenauser.Data;
Step 1 - (Creation of data class- this will be a .cs file)
This can be placed in the Data folder
using System;
using System.Collections.Generic;
// needed for validations
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
//needed for the schema
using System.ComponentModel.DataAnnotations.Schema;
namespace Payarenauser.Data { // i am using a database in postgres sql
[Table("bank", Schema = "public")]
public class BankClass {
[Key]
public int bankid {
get;
set;
}
public string bankcode {
get;
set;
}
public string bankabbr {
get;
set;
}
public string bankfullname {
get;
set;
}
}
}
Step 2 - (Creation of a db context file- This is also a .cs file)
DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Payarenauser.Data;
using System;
using System.Collections.Generic;
using System.Text;
namespace payarenauser.Data {
public class ApplicationDbContext1: DbContext {
public ApplicationDbContext1(DbContextOptions < ApplicationDbContext1 > options): base(options) {}
public DbSet < BankClass > Bank {
get;
set;
}
}
}
Step 3 - (Creation of a Service- This is also a .cs file)
A service is meant to define the logic for CRUD Operations, create a folder called Service and place the file inside.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using payarenauser.Data;
using Payarenauser.Data;
namespace Payarenauser.Services {
public class BankService {
protected readonly ApplicationDbContext1 _dbcontext1;
public BankService(ApplicationDbContext1 _db) {
_dbcontext1 = _db;
}
public List < BankClass > GetAllBanks() {
return _dbcontext1.Bank.ToList();
}
public List < BankClass > GetuniqueBank(string bankname) {
return _dbcontext1.Bank.Where(x => x.bankfullname == bankname).ToList();
}
public List < BankClass > GetexceptBank(string bankname) {
return _dbcontext1.Bank.Where(x => x.bankfullname != bankname).ToList();
}
}
}
Now let's look at the code above in detail.
First, we import the necessary namespaces.
Next, we bring in the DB context
Next, we make the user the Service scope as public and initiate the DB context to a DB
We can now go into the main code, the main reason for this article.
We have to create a list as a method and pass parameters to it, this handles the logic for filtering.
In the code above two filtering options were created.




Join the conversation! Your thoughts help the community grow.