Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:36:28+00:00 2026-06-09T22:36:28+00:00

I need the gridview header to be freezed while scrolling. So i got a

  • 0

I need the gridview header to be freezed while scrolling. So i got a script which works in normal pages. But while using Master pages it acts like a normal grid which has a scroll. what should i change in the script to make it work in master pages.?

<script type="text/javascript" language="javascript">

            var GridId = "<%= GridView1.ClientID %>";
            var ScrollHeight = 300;
            window.onload = function () {
                var grid = document.getElementById(GridId);
                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);
                var scrollableDiv = document.createElement("div");
                if (parseInt(gridHeight) > ScrollHeight) {
                    gridWidth = parseInt(gridWidth) + 17;
                }
                scrollableDiv.style.cssText = "overflow:auto;height:" + ScrollHeight + "px;width:" + gridWidth + "px";
                scrollableDiv.appendChild(grid);
                parentDiv.appendChild(scrollableDiv);
            }

    </script>


 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
            <Columns
                <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:BoundField DataField="Country" HeaderText="Country" />
            </Columns>
        </asp:GridView>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:36:29+00:00Added an answer on June 9, 2026 at 10:36 pm

    This can be achieved using JQuery. A little bit of changes in the above scripts done this. got the output.

    // Put the below code in a .js file

     (function ($) {
            $.fn.Scrollable = function (options) {
                var defaults = {
                    ScrollHeight: 300,
                    Width: 0
                };
                var options = $.extend(defaults, options);
                return this.each(function () {
                    var grid = $(this).get(0);
                    var gridWidth = grid.offsetWidth;
                    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");
                    gridWidth = parseInt(gridWidth) + 17;
                    scrollableDiv.style.cssText = "overflow:auto;height:" + options.ScrollHeight + "px;width:" + gridWidth + "px";            
                    scrollableDiv.appendChild(grid);
                    parentDiv.appendChild(scrollableDiv);
                });
            };
        })(jQuery);
    

    // Add this small piece of code in the page.

     <script type="text/javascript" language="javascript">
            $(document).ready(function () {
                $('#<%=this.grdDisplay.ClientID %>').Scrollable();
            }
                )
        </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to select all but the first row (header) in all tables (GridView)
I need a GridView, but in each grid, there will be an ImageView and
i need to set image as background in android gridview. I have code which
I have seen some gridview header freezing examples on the web but I have
How does FindControl method works if I need to find any Control which is
? Need fixed Header Row from Vertical Scroll bar, because my GridView is very
I have gridview with 15 and above columns, I need to freeze the header
I need a GridView with rows with different height. Only for rows, not for
I need to export a gridview to excel, I put the return html code
Need some help with DataFormatString in GridView. I have a Double value that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.