Click here to Skip to main content
15,885,757 members
Articles / Web Development / ASP.NET
Tip/Trick

Searchable Gridview using Jquery - Easiest Way

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
7 Jun 2017CPOL1 min read 45.4K   23   7
The tip provides you the code for implementing Search in your gridview using Jquery.

Introduction

This tip will help you in learning how to make the grid searchable at the client side, the search will be accurate and faster irrespective of any datatype since the jquery is used to perform the task.

Background

This tip provides you with the Jquery method which returns the matching grid row on entering the desired search values into the search textbox.

Benefits of using this code are as follows:

  • It provides faster search
  • Any type of data can be searched throughout the gridview

Using the Code

The code consists of the following.

Jquery method which takes two inputs:

  • First is search textbox - txtSearch
  • Secondly, the Gridview in which the search is to be done - grd

Let the gridview and search textbox be as follows:

ASP.NET
<asp:Textbox runat="server" ID="txtSearch"></asp:Textbox>

<asp:GridView ID="grd" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="id" HeaderText="id">
    <asp:BoundField DataField="id" HeaderText="id">
</Columns>
</asp:GridView>

Please find out the method for searching throughout the grid as follows:

JavaScript
function SearchGrid(txtSearch, grd)
{
    if ($("[id *=" + txtSearch + " ]").val() != "") {
        $("[id *=" + grd + " ]").children
        ('tbody').children('tr').each(function () {
            $(this).show();
        });
        $("[id *=" + grd + " ]").children
        ('tbody').children('tr').each(function () {
            var match = false;
            $(this).children('td').each(function () {
                if ($(this).text().toUpperCase().indexOf($("[id *=" + 
            txtSearch + " ]").val().toUpperCase()) > -1) {
                    match = true;
                    return false;
                }
            });
            if (match) {
                $(this).show();
                $(this).children('th').show();
            }
            else {
                $(this).hide();
                $(this).children('th').show();
            }
        });


        $("[id *=" + grd + " ]").children('tbody').
                children('tr').each(function (index) {
            if (index == 0)
                $(this).show();
        });
    }
    else {
        $("[id *=" + grd + " ]").children('tbody').
                children('tr').each(function () {
            $(this).show();
        });
    }
}

In the above code, searchGrid(txtSearch,grd) the method is called by passing the textbox in which the search value is entered and the second parameter consists of the grid on which the search is to be carried out.

JavaScript
$(document).on("Keyup",function(){
    SearchGrid('<%=txtSearch.ClientID%>','<%=grd.ClientID%>');
});

Points of Interest

The above code is the simplest way to carry out a search through the gridview It can be used for searching any type of parameter, whether,datetime integer or string and since it is carried out on the client side, the search is very fast as compared to the other search.

Thank you very much for reading!! Please vote if you found it helpful!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionif the grid has paging? Pin
VanTamsir8-Jun-17 20:05
VanTamsir8-Jun-17 20:05 
AnswerRe: if the grid has paging? Pin
Gunaprasad Shetty9-Jun-17 0:03
Gunaprasad Shetty9-Jun-17 0:03 
GeneralProvide Attachment Pin
Ravi Sargam7-May-16 19:51
Ravi Sargam7-May-16 19:51 
QuestionPerformance Pin
P Raman14-Feb-16 20:38
P Raman14-Feb-16 20:38 
PraiseSo simple! Pin
Chris Schaller11-Nov-15 1:06
professionalChris Schaller11-Nov-15 1:06 
QuestionPlease add an example Pin
Member 790852918-Feb-15 9:49
Member 790852918-Feb-15 9:49 
AnswerRe: Please add an example Pin
Gunaprasad Shetty18-Feb-15 18:28
Gunaprasad Shetty18-Feb-15 18:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.