Hi tareq24bd,
Check this example. Now please take its reference and correct your code.
HTML
Search:<asp:TextBox ID="txtSearch" runat="server" />
<asp:Button Text="Search" runat="server" OnClick="OnSearch" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
VB.Net
Imports System.Data
Imports System.Data.OleDb
Imports System.Text.RegularExpressions
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
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);
}
}
protected void OnSearch(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\\Users\\developer4\\Desktop\\Database1.accdb";
using (OleDbConnection con = new OleDbConnection(constr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.CommandText = "SELECT * FROM Employees WHERE Name LIKE '%' + @Search + '%' OR City LIKE '%' + @Search + '%' OR Country LIKE '%' + @Search + '%'";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Search", txtSearch.Text.Trim());
DataTable dt = new DataTable();
using (OleDbDataAdapter sda = new OleDbDataAdapter(cmd))
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
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
Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGrid()
End Sub
Private Sub BindGrid()
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\\Users\\developer4\\Desktop\\Database1.accdb"
Using con As OleDbConnection = New OleDbConnection(constr)
Using cmd As OleDbCommand = New OleDbCommand()
cmd.CommandText = "SELECT * FROM Employees WHERE Name LIKE '%' + @Search + '%' OR City LIKE '%' + @Search + '%' OR Country LIKE '%' + @Search + '%'"
cmd.Connection = con
cmd.Parameters.AddWithValue("@Search", txtSearch.Text.Trim())
Dim dt As DataTable = New DataTable()
Using sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot
Access db file
Output