This article will show you how you can search and highlight the text in a gridview control in asp.net using c#. This i have shown on button click event.
So for this article first we will create a new asp.net application and add a textbox, Button control and a gridview control.
Now we need to use the below code to highlight the text.
In above code i have highlighted the search text on row data bound. So please check the code.
So for this article first we will create a new asp.net application and add a textbox, Button control and a gridview control.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SEarchAndHeilight.aspx.cs" Inherits="WebApplication7.SEarchAndHeilight" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Highlight Search
Text in Gridview Asp.net C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Search
Text:
<asp:TextBox ID="txtSearchText"
runat="server"></asp:TextBox><asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /><br />
<br />
<asp:GridView ID="GridView1"
runat="server" OnRowDataBound="GridView1_RowDataBound" Width="100%"></asp:GridView>
</div>
</form>
</body>
</html>
|
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using
System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class SEarchAndHeilight :
System.Web.UI.Page
{
string searchText = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt
= GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
public DataTable GetData()
{
DataTable _objdt = new DataTable();
_objdt.Columns.Add("StudentName", typeof(string));
_objdt.Columns.Add("Address", typeof(string));
var _objrow = _objdt.NewRow();
_objrow["StudentName"] = "Rajesh
Singh";
_objrow["Address"] = "Address
1";
_objdt.Rows.Add(_objrow);
_objrow = _objdt.NewRow();
_objrow["StudentName"] = "Rakesh";
_objrow["Address"] = "Address
2";
_objdt.Rows.Add(_objrow);
_objrow = _objdt.NewRow();
_objrow["StudentName"] = "Vinay
Singh";
_objrow["Address"] = "Address
3";
_objdt.Rows.Add(_objrow);
_objrow = _objdt.NewRow();
_objrow["StudentName"] = "Yougesh
Singh";
_objrow["Address"] = "Address
4";
_objdt.Rows.Add(_objrow);
return _objdt;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt =
GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//For
row 1
e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, txtSearchText.Text.Trim(),
delegate(Match match)
{
return
string.Format("<span style =
'background-color:red'>{0}</span>", match.Value);
},
RegexOptions.IgnoreCase);
//For row 2
e.Row.Cells[1].Text = Regex.Replace(e.Row.Cells[1].Text, txtSearchText.Text.Trim(),
delegate(Match match)
{
return
string.Format("<span style =
'background-color:red'>{0}</span>", match.Value);
},
RegexOptions.IgnoreCase);
}
}
}
}
|
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//For
row 1
e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, txtSearchText.Text.Trim(),
delegate(Match match)
{
return
string.Format("<span style =
'background-color:red'>{0}</span>", match.Value);
},
RegexOptions.IgnoreCase);
//For row 2
e.Row.Cells[1].Text = Regex.Replace(e.Row.Cells[1].Text, txtSearchText.Text.Trim(),
delegate(Match match)
{
return
string.Format("<span style =
'background-color:red'>{0}</span>", match.Value);
},
RegexOptions.IgnoreCase);
}
}
}
}
|
In above code i have search for two rows. Because in example i have taken only two rows. You can perform this as many no of rows you want the highlight. Now we have done run the application to check the output.
0 comments:
Please let me know your view