SearchAndHighLightKeyWords

Thread is closed for posting
6 posts, 0 answers
  1. E69AACB4-3C5B-42BC-9BDD-D0F081D8FE34
    E69AACB4-3C5B-42BC-9BDD-D0F081D8FE34 avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Dec 2007 Link to this post

    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.
  2. C7498A83-7E2E-418C-8791-93EF573A7569
    C7498A83-7E2E-418C-8791-93EF573A7569 avatar
    9934 posts
    Member since:
    Nov 2016

    Posted 18 Dec 2007 Link to this post

    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
  3. B7D5EC7D-596A-47C2-9995-892A6304521A
    B7D5EC7D-596A-47C2-9995-892A6304521A avatar
    25 posts
    Member since:
    Mar 2009

    Posted 26 Nov 2009 Link to this post

    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);  
                            }  
                              
                        }  
                    }  
                } 
  4. C7498A83-7E2E-418C-8791-93EF573A7569
    C7498A83-7E2E-418C-8791-93EF573A7569 avatar
    9934 posts
    Member since:
    Nov 2016

    Posted 27 Nov 2009 Link to this post

    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.
  5. AFB1F126-7586-4677-88F7-D0F595FB7DAA
    AFB1F126-7586-4677-88F7-D0F595FB7DAA avatar
    14 posts
    Member since:
    Feb 2011

    Posted 31 Mar 2011 Link to this post

    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
  6. 8F7B74E1-4CC0-465A-81F1-8ACA458A0FDD
    8F7B74E1-4CC0-465A-81F1-8ACA458A0FDD avatar
    21 posts
    Member since:
    Nov 2012

    Posted 24 Jul 2013 Link to this post

    Does it work with template columns containing load on demand radcomboboxes?
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.