Filter ASP.Net GridView records using TextBox in HeaderRow with Fixed header in jQuery

Last Reply 2 months ago By pandeyism

Posted 3 months ago

Hi All,

With reference to below link is it possible to add verticle scrollbar with fixed header.

https://www.aspforums.net/Threads/108595/Filter-GridView-based-on-GridView-Header-TextBox-with-Paging-and-Sorting-without-using-Stored-Procedure-in-ASPNet

 

I have tried to add below code but when i scroll it also scrolls header also. However horizontal scrollbar automatically comes with "table-responsive" class.

 <div style="height: 400px; overflow: auto">
<%Grdiview here%>
</div>

 

Posted 2 months ago

Hey Amol111,

If you want to responsive features with fixed header, searching and scrollbar so instead of gridview use repeater and apply on that jquery Datatable plugin.

Please refer below sample.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.4.2/css/buttons.dataTables.min.css" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#example thead th').each(function () {
            var title = $('#example thead th').eq($(this).index()).text();
            $(this).find(".filter-wrapper").html('<input type="text" class="filter form-control" placeholder="' + title + '" />');
        });
        var otable = $('#example').DataTable({ "bPaginate": false, "sScrollY": "200" });
        // Apply the search
        otable.columns().every(function () {
            var that = this;
            $('input', this.header()).on('keyup change click', function (e) {
                e.stopPropagation();
                if (that.search() !== this.value) {
                    that.search(this.value).draw();
                }
            });
        });
    });
</script>
<asp:Repeater ID="rptCustomers" runat="server" ClientIDMode="Static">
    <HeaderTemplate>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>
                        Id<div class="filter-wrapper">
                    </th>
                    <th>
                        Name<div class="filter-wrapper">
                    </th>
                    <th>
                        Country<div class="filter-wrapper">
                    </th>
                </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
            </td>
            <td>
                <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
            </td>
            <td>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody> </table>
    </FooterTemplate>
</asp:Repeater>

Namespaces

C#

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = GetData();
        rptCustomers.DataSource = dt;
        rptCustomers.DataBind();
    }
}

private static DataTable GetData()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT CustomerID,ContactName,Country FROM Customers";
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                return dt;
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As DataTable = GetData()
        rptCustomers.DataSource = dt
        rptCustomers.DataBind()
    End If
End Sub

Private Shared Function GetData() As DataTable
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT CustomerID,ContactName,Country FROM Customers"
    Dim cmd As SqlCommand = New SqlCommand(query)
    Using con As SqlConnection = New SqlConnection(conString)

        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dt As DataTable = New DataTable()
                sda.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function

Screenshot