Highlight Search in Gridview
Hi all
Try this example to Highlight Search in Gridview:
.aspx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| <head runat="server"> <title>Untitled Page</title> <style type="text/css"> .highlight {text-decoration:none; font-weight:bold;color:black; background:yellow;} </style> </head> <body> <form id="form1" runat="server"> <div> Enter first name to search: </div> <div> <asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" OnTextChanged="txtSearch_TextChanged"> </asp:TextBox> <br /> </div> <div> <asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"> <Columns> <asp:TemplateField HeaderText="FirstName"> <ItemTemplate> <asp:Label ID="lblFirstName" runat="server" Text='<%# Highlight(Eval("FirstName").ToString()) %>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LastName"> <ItemTemplate> <asp:Label ID="lblLastName" runat="server" Text='<%#(Eval("LastName")) %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <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" /> <EmptyDataTemplate> No data found </EmptyDataTemplate> </asp:GridView> </div> </form> </body> </html> |
in code Behind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
| 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) { BindGrid(); } } private DataTable GetRecords() { SqlConnection conn = new SqlConnection(strConnection); conn.Open(); 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(); dAdapter.Fill(objDs); return objDs.Tables[0]; } private void BindGrid() { DataTable dt = GetRecords();if (dt.Rows.Count > 0) { grdSearch.DataSource = dt; grdSearch.DataBind(); } } 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; grdSearch.DataBind(); } 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) { SearchText(); } } |
Good Luck
Post a Comment