Click here to Skip to main content
12,760,251 members (32,119 online)
Click here to Skip to main content
Add your own
alternative version

Stats

11.7K views
488 downloads
12 bookmarked
Posted 3 Sep 2014

Freeze Gridview Header Using JQuery

, 3 Sep 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to freeze gridview header using JQuery

Sample Image - maximum width is 600 pixels

Introduction

We may know many scripts to fix the gridview header. Personally, I feel some of the scripts are a little bit difficult to understand and some scripts are not working well in paging and sorting.

Here, I suggest one of the simple jquery for freeze Gridview header which will work well in paging and sorting too.

Using the Code

In ASP design page, just create two grids:

  • dgvHeader (we are going to use for freeze header)
  • dgvSample (this is actual grid to bind data)

Sample Image - maximum width is 600 pixels

Bind data into grid using data bind methods.

JQuery

See the below Jquery for fix header:

    (function ($) {
    $.fn.Scrollable = function (options) {
        var defaults = {
            ScrollHeight: 200, // to set height of grid 
            Width:305 // to set width of grid Default value is 0 
        };
        var options = $.extend(defaults, options);
        return this.each(function () {
            var grid = $(this).get(0);
            var gridWidth = grid.offsetWidth;
            var gridHeight = grid.offsetHeight;
            var headerCellWidths = new Array();
            for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) {
                headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth;
            }
            grid.parentNode.appendChild(document.createElement("div"));
            var parentDiv = grid.parentNode;

            var table = document.createElement("table");
            for (i = 0; i < grid.attributes.length; i++) {
                if (grid.attributes[i].specified && grid.attributes[i].name != "id") {
                    table.setAttribute(grid.attributes[i].name, grid.attributes[i].value);
                }
            }
            table.style.cssText = grid.style.cssText;
            table.style.width = gridWidth + "px";
            table.appendChild(document.createElement("tbody"));
            table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]);
            var cells = table.getElementsByTagName("TH");

            var gridRow = grid.getElementsByTagName("TR")[0];
            for (var i = 0; i < cells.length; i++) {
                var width;
                if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {
                    width = headerCellWidths[i];
                }
                else {
                    width = gridRow.getElementsByTagName("TD")[i].offsetWidth;
                }
                cells[i].style.width = parseInt(width-3) + "px";
                gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width-3) + "px";
            }
            parentDiv.removeChild(grid);

            var dummyHeader = document.createElement("div");
            dummyHeader.appendChild(table);
            parentDiv.appendChild(dummyHeader);
            if (options.Width > 0) {
                gridWidth = options.Width;
            }
            var scrollableDiv = document.createElement("div");
            if (parseInt(gridHeight) > options.ScrollHeight) {
                gridWidth = parseInt(gridWidth) + 17;
            }
            scrollableDiv.style.cssText = "overflow:auto;height:" + 
            options.ScrollHeight + "px;width:" + gridWidth + "px";
            scrollableDiv.appendChild(grid);
            parentDiv.appendChild(scrollableDiv);
        });
    };
})(jQuery);    

Call the Jquery into ASP Design page header and pass the grid names as a parameter:

$(document).ready(function () {  // Datagrid Header Freeze
    $("#<%=dgvHeader.ClientID %>").Scrollable({
        ScrollHeight: 200
    });
    $("#<%=dgvSample.ClientID %>").Scrollable({
        ScrollHeight: 200
    });
});

We can change the height using scrollHeight parameter.
This will work well in both horizontal and vertical scroll for vertical scroll.

Sample Image - maximum width is 600 pixels

Sample Image - maximum width is 600 pixels

Reference

I referred to this site to fix the issue:

License

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

Share

About the Author


You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionAlignment Issue in the cols Pin
nishant00927-Jul-16 2:39
membernishant00927-Jul-16 2:39 
Questionsolution Pin
boss199927-Jul-15 1:37
memberboss199927-Jul-15 1:37 
AnswerRe: solution Pin
nishant00931-Jul-16 3:50
membernishant00931-Jul-16 3:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 3 Sep 2014
Article Copyright 2014 by Santhosh Babu Mahimairaj
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid