Thread is closed for posting
6 posts, 0 answers
  1. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Dec 2007 Link to this post


    RadGrid version


    .NET version


    Visual Studio version


    programming language


    browser support

    all browsers supported by RadGrid


    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).

    <%@ 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" ""> 
    <html xmlns="" > 
    <head runat="server">  
        <title>Untitled Page</title> 
       <style type="text/css">  
    .highlight {text-decoration:none; font-weight:bold; color:black; background:yellow;}  
        <form id="form1" runat="server">  
            <radG:RadGrid ID="RadGrid1" PageSize="2" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender" Skin="Green">  
                    <Pdf PageBottomMargin="" PageFooterMargin="" PageHeaderMargin="" PageHeight="11in" 
                        PageLeftMargin="" PageRightMargin="" PageTopMargin="" PageWidth="8.5in" /> 
                        <radG:GridBoundColumn DataField="ProductName" HeaderText="ProductName" UniqueName="ProductName">  
                        <radG:GridBoundColumn DataField="ProductID" HeaderText="ProductID" UniqueName="ProductID">  
                        <radG:GridBoundColumn DataField="SupplierID" HeaderText="SupplierID" UniqueName="SupplierID">  
                    <ExpandCollapseColumn Resizable="False" Visible="False">  
                        <HeaderStyle Width="20px" /> 
                    <RowIndicatorColumn Visible="False">  
                        <HeaderStyle Width="20px" /> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" /></div>  

    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);  
            SqlDataAdapter adp = new SqlDataAdapter("select * from Products", conn);  
            DataTable dtCurrentData = new DataTable();  
            RadGrid1.DataSource = dtCurrentData;  
        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)  
        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>";  

  2. Sebastian
    Sebastian avatar
    9934 posts

    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,
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Imran
    Imran 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);  
                            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. Sebastian
    Sebastian avatar
    9934 posts

    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,
    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. ashes
    ashes 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. Bernard
    Bernard 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.