Search and Filter records in GridView using ASP.NET FilterParameters in C# and VB.NET

← PrevNext →

Last updated: 10th May 2026

Displaying large amounts of data in an ASP.NET GridView allows you to organize and present information in a clean, structured, and user-friendly format. However, as datasets grow, users often need a quick way to search and filter records, especially when GridView paging is enabled. In this tutorial, you'll learn how to search and filter records in an ASP.NET GridView using the FilterParameters property of the SqlDataSource control. This simple and efficient approach helps improve data navigation, usability, and overall application performance.

Search records in GridView using FilterParameters

See this demo

A general syntax of FilterParameters attribute will look like this.

<FilterParameters>
    ...
</FilterParameters>

Create a table in SQL Server

We need data. Therefore, before starting with our page design, we need to create a table in SQL Server database. The name of the table is "dbo.Books". I already have a sample table with a list of books, designed exclusively for these situations. It will spare us from creating it repeatedly.

The HTML and GridView

Open visual studio and add a new web site. In the default page add a GridView control and attach it to SqlDataSource control. See the markup below.

<!DOCTYPE html>
<html>
<head>
    <title>Search Data in GridView Control using ASP.NET</title>
</head>
<body>
    <form>
        <div>
            <h3>Search Data in GridView</h3>

            <asp:GridView ID="GridView" 
                DataSourceID="SqlDataSource1" 
                CellPadding="5" CellSpacing="0" 
                AllowPaging="true" PageSize="5" 
                OnRowCreated="GridView_RowCreated"
                runat="server">

                <HeaderStyle BackColor="#989898" ForeColor="white" /></asp:GridView>

                <asp:SqlDataSource
                    ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:YOURDBConnectionString %>"
                
                    SelectCommand="SELECT [BookID], [BookName], 
                        [Category], [Price] FROM dbo.[Books]"
                            FilterExpression="[BookName] LIKE '%{0}%'">

                    <FilterParameters
                        <asp:ControlParameter Name="BookName" 
                            ControlID="txtFind" PropertyName="Text" />
                    </FilterParameters>
                </asp:SqlDataSource>
            <br />

            Enter Search Value: 
            <asp:TextBox ID="txtFind" runat="server"></asp:TextBox>
            <input type="submit" id="btSubmit" runat="server" />

            <%--LABEL TO SHOW ROW COUNT.--%>
            <div style="clear:both;padding:10px 0;">
                <label id="msg" runat="server"></label>
            </div>
        </div>
    </form>
</body>
</html>

I also have a textbox control, along with a "button" and "label" control.

See this demo

The label will show us the returned number of rows, when the FilterParameters property finds keywords matching with the entered values in the search textbox.

There are two important properties in the above markup that we want you to pay attention.

01) The FilterExpression property in the SqlDataSource attributes.
02) The <FilterParameters> attribute.

The "FilterExpression" property contains a placeholder for the filter parameter "BookName", which is contained in one of the collections (ControlParameters) of <FilterParameters> attribute.

The second column of the GridView control will display the "BookName" field with values. I have applied the filter on the second column and the search result will show records that will match the value entered in the search textbox.

In addition, I have set paging as true (AllowPaging="true"), and the page size to "five". This will allow us to check if it searches for keywords on every page and returns the desired result.

C# Code

We are not doing much at the code behind level, and it is "optional". All we have is a GridView RowCreated event, which will get the number of rows found (row count) that match the search keyword.

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

partial class _Default : System.Web.UI.Page
{

    protected void GridView_RowCreated(object sender, 
        System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (!string.IsNullOrEmpty(txtFind.Text))
        {
            msg.InnerText = "Found " + GridView.Rows.Count + 
                " rows matching keyword '" + txtFind.Text + "'.";
        }
    }
}
VB.NET
Option Explicit On

Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub GridView_RowCreated(sender As Object, 
            e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If Trim(txtFind.Text) <> "" Then
            msg.InnerText = "Found " & GridView.Rows.Count & _
                " rows matching keyword '" & txtFind.Text & "'."
        End If
    End Sub
End Class
See this demo
Conclusion

The SqlDataSource FilterParameters property provides a simple and efficient way to search and filter records in an ASP.NET GridView. It helps improve data navigation and enhances the user experience, especially when working with paging-enabled GridView controls. Test this approach with larger datasets to see how effectively it performs in real-world ASP.NET applications.

💡 Although newer ASP.NET Core applications typically use LINQ and Entity Framework for filtering, this method remains useful for existing Web Forms projects.

← PreviousNext →