Search Records in GridView and Highlight Results in Asp.Net

Search Records in GridView and Highlight Results in Asp.Net

In this tutorial we will learn how to Search Records in GridView and Highlight Results in Asp.Net. Here we will use a search textbox, button and display and highlight results in the gridview. In earlier post we have discussed Total of Columns in GridView Footer in Asp.Net.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>Search Records in GridView and Highlight Results in Asp.Net</title>
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
.highlight {text-decoration: none;color:black;background:yellow;}
<form id="form1" runat="server">
<h1>Search Records in GridView and Highlight Results in Asp.Net</h1>
Enter first name to search:<asp:TextBox ID="txtSearch" runat="server" />
<asp:Button ID="btnsearch" runat="server" OnClick="btnsearch_Click" Text="Search" />
<br />

<asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="false" Width="100%">
<asp:TemplateField HeaderText="User name">
<asp:Label ID="lbluName" runat="server" Text='<%# Highlight(Eval("username").ToString()) %>'/>
<asp:TemplateField HeaderText="User Email">
<asp:Label ID="lblemail" runat="server" Text='<%#(Eval("uemail")) %>'/>

<br /><br />
<br /><br />
All rights reserved by <a href=""></a> |
Hosting partner <a href="" target="_blank">Grootstech</a>    


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
    String strcon = ConfigurationManager.ConnectionStrings["connnn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)
    private DataTable GetRecords()
        SqlConnection conn = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select top(10) * from users";
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        DataSet objDs = new DataSet();
        return objDs.Tables[0];

    private void BindGrid()
        DataTable dt = GetRecords();
        if (dt.Rows.Count > 0)
            grdSearch.DataSource = dt;
    private void SearchText()
        DataTable dt = GetRecords();
        DataView dv = new DataView(dt);
        string SearchExpression = null;
        if (!String.IsNullOrEmpty(txtSearch.Text))
            SearchExpression = string.Format("{0} '%{1}%'",
            grdSearch.SortExpression, txtSearch.Text);

        dv.RowFilter = "Username like" + SearchExpression;
        grdSearch.DataSource = dv;

    public string Highlight(string InputTxt)
        string Search_Str = txtSearch.Text.ToString();
        Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
        return RegExp.Replace(InputTxt,
        new MatchEvaluator(ReplaceKeyWords));
        RegExp = null;

    public string ReplaceKeyWords(Match m)
        return "<span class=highlight>" + m.Value + "</span>";

    protected void btnsearch_Click(object sender, EventArgs e)


Search Records in GridView and Highlight Results in Asp.Net