Home > ASP.Net > Highlight Search in Gridview

Highlight Search in Gridview

Hi all

Try this example  to Highlight Search in Gridview:



<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.highlight {text-decoration:none; font-weight:bold;color:black; background:yellow;}
<body> <form id="form1" runat="server">
Enter first name to search:
<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" OnTextChanged="txtSearch_TextChanged">
</asp:TextBox>&nbsp;<br />
</div> <div>
 <asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333">
<asp:TemplateField HeaderText="FirstName">
<asp:Label ID="lblFirstName" runat="server" Text='<%# Highlight(Eval("FirstName").ToString()) %>'>
<asp:TemplateField HeaderText="LastName">
<asp:Label ID="lblLastName" runat="server" Text='<%#(Eval("LastName")) %>'></asp:Label>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
No data found

in code Behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
string strConnection = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

private DataTable GetRecords()
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from Employees";
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 = "FirstName like" + SearchExpression;
grdSearch.DataSource = dv;

public string Highlight(string InputTxt)
string Search_Str = txtSearch.Text.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
// Set the RegExp to null.
RegExp = null;

public string ReplaceKeyWords(Match m)
return "<span class=highlight>" + m.Value + "</span>";
protected void txtSearch_TextChanged(object sender, EventArgs e)

Good Luck

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: