Tuesday, 18 October 2016

Highlight Search Text in Gridview Asp.net C#

10/18/2016 - By Pranav Singh 0

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.

<%@ 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>


Now we need to use the below code to highlight the text.

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);
            }
        }

    }
}

In above code i have highlighted the search text on row data bound. So please check the code.

        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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top