Hi tareq24bd,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Search:<asp:TextBox ID="txtSearch" runat="server" />
<asp:Button Text="Search" runat="server" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
DataSourceID="GridDataSource">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT FirstName + ' ' + LastName 'Name',City,Country FROM Employees"
FilterExpression="Name LIKE '%{0}%' OR City LIKE '%{0}%' OR Country LIKE '%{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
Namespaces
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
Code
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, txtSearch.Text.Trim(), delegate (Match match)
{
return string.Format("<span style = 'background-color:#FFFF0C'>{0}</span>", match.Value);
}, RegexOptions.IgnoreCase);
e.Row.Cells[1].Text = Regex.Replace(e.Row.Cells[1].Text, txtSearch.Text.Trim(), delegate (Match match)
{
return string.Format("<span style = 'background-color:#FFFF0C'>{0}</span>", match.Value);
}, RegexOptions.IgnoreCase);
e.Row.Cells[2].Text = Regex.Replace(e.Row.Cells[2].Text, txtSearch.Text.Trim(), delegate (Match match)
{
return string.Format("<span style = 'background-color:#FFFF0C'>{0}</span>", match.Value);
}, RegexOptions.IgnoreCase);
}
}
VB.Net
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(0).Text = Regex.Replace(e.Row.Cells(0).Text, txtSearch.Text.Trim(),
Function(ByVal match As Match) String.Format("<span style = 'background-color:#FFFF0C'>{0}</span>",
match.Value), RegexOptions.IgnoreCase)
e.Row.Cells(1).Text = Regex.Replace(e.Row.Cells(1).Text, txtSearch.Text.Trim(),
Function(ByVal match As Match) String.Format("<span style = 'background-color:#FFFF0C'>{0}</span>",
match.Value), RegexOptions.IgnoreCase)
e.Row.Cells(2).Text = Regex.Replace(e.Row.Cells(2).Text, txtSearch.Text.Trim(),
Function(ByVal match As Match) String.Format("<span style = 'background-color:#FFFF0C'>{0}</span>",
match.Value), RegexOptions.IgnoreCase)
End If
End Sub
Screenshot