Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > Grid > SearchAndHighLightKeyWords

Not answered SearchAndHighLightKeyWords

Feed from this thread
  • Posted on Dec 12, 2007 (permalink)

    Requirements

    RadGrid version

    5.0.1

    .NET version

    2.x

    Visual Studio version

    2005

    programming language

    C#

    browser support

    all browsers supported by RadGrid



    PROJECT DESCRIPTION

    This sample project explains how to highlight a string defined through a textbox control inside any given cell that holds that reference (highlight a search word that appears inside RadGrid).

    ASPX:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
     
    <%@ Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="radG" %> 
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <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> 
            <radG:RadGrid ID="RadGrid1" PageSize="2" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender" Skin="Green">  
                <ExportSettings> 
                    <Pdf PageBottomMargin="" PageFooterMargin="" PageHeaderMargin="" PageHeight="11in" 
                        PageLeftMargin="" PageRightMargin="" PageTopMargin="" PageWidth="8.5in" /> 
                </ExportSettings> 
                <MasterTableView> 
                    <Columns> 
                        <radG:GridBoundColumn DataField="ProductName" HeaderText="ProductName" UniqueName="ProductName">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="ProductID" HeaderText="ProductID" UniqueName="ProductID">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="SupplierID" HeaderText="SupplierID" UniqueName="SupplierID">  
                        </radG:GridBoundColumn> 
                    </Columns> 
                    <ExpandCollapseColumn Resizable="False" Visible="False">  
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <RowIndicatorColumn Visible="False">  
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                </MasterTableView> 
            </radG:RadGrid> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" /></div>  
        </form> 
    </body> 
    </html> 
     


    CS:
    using System;  
    using System.Data;  
    using System.Configuration;  
    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 Telerik.WebControls;  
    using System.Text.RegularExpressions;  
     
    public partial class _Default : System.Web.UI.Page   
    {  
         
        protected void Page_Load(object sender, EventArgs e)  
        {  
     
        }  
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            string connectionstring = "Data Source=(local); Initial Catalog=NorthWind;User ID=sa; Password=sa";  
            SqlConnection conn = new SqlConnection(connectionstring);  
            conn.Open();  
            SqlDataAdapter adp = new SqlDataAdapter("select * from Products", conn);  
            DataTable dtCurrentData = new DataTable();  
            adp.Fill(dtCurrentData);  
            RadGrid1.DataSource = dtCurrentData;  
            conn.Close();  
              
        }  
        protected void RadGrid1_PreRender(object sender, EventArgs e)  
        {  
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)  
            {  
                foreach (GridColumn col in RadGrid1.MasterTableView.Columns)  
                {  
                   item[col.UniqueName].Text = Highlight(TextBox1.Text, item[col.UniqueName].Text);  
                }  
            }   
     
        }  
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            RadGrid1.Rebind();  
        }  
     
        public string Highlight(string Search_Str, string InputTxt)  
        {  
     
        // 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.  
         
        }  
     
        public string ReplaceKeyWords(Match m)  
        {  
     
            return "<span class=highlight>" + m.Value + "</span>";  
     
        }  
    }  
     


    Princy.

    Reply

  • Sebastian Sebastian admin's avatar

    Posted on Dec 18, 2007 (permalink)

    Hi Princy,

    Thank you for posting this example in the code library section on our site. It can be surely helpful for other people which are looking for similar "search and highlight results" functionality for their grids. I have updated your Telerik points for the involvement.

    Best regards,
    Stephen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center

    Reply

  • Imran avatar

    Posted on Nov 26, 2009 (permalink)

    Great article saved me a bunch of work.

    One little thing though, this does not work for grids with template columns.

    If there are Hyperlink or Label controls in the template column the pre-render can be modified like this to accomplish the same effect.

    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)  
                {  
                    foreach (GridColumn col in RadGrid1.MasterTableView.Columns)  
                    {  
                        if (item[col.UniqueName].Controls.Count == 0)  
                        {  
                            item[col.UniqueName].Text = Misc.Highlight(TextBox1.Text, item[col.UniqueName].Text);  
                        }  
                        else  
                        {  
     
                            foreach (HyperLink HLink in item[col.UniqueName].Controls.OfType<HyperLink>())  
                            {  
                                HLink.Text = Misc.Highlight(TextBox1.Text, HLink.Text);  
                            }  
                            foreach (Label lbl in item[col.UniqueName].Controls.OfType<Label>())  
                            {  
                                lbl.Text = Misc.Highlight(TextBox1.Text, lbl.Text);  
                            }  
                              
                        }  
                    }  
                } 

    Reply

  • Sebastian Sebastian admin's avatar

    Posted on Nov 27, 2009 (permalink)

    Hello Imran,

    Thank you for posting your extension of the sample code library project provided by Princy. Thus you can help other Telerik community members who have template columns with controls inside them and would like to highlight their text in the way demonstrated here. I updated your Telerik points for the post.

    Kind regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

    Reply

  • ashes avatar

    Posted on Mar 31, 2011 (permalink)

    how to apply same functionality in mvc grid ? Please give some Code Samples and Demos. Its very urgent as my project is in final stages and I am a newbee in ASP.NET MVC and Telerik MVC Extensions

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > Grid > SearchAndHighLightKeyWords