How to Freeze Header in ASP.NET Gridview using C#

BeUnique 2,212 Reputation points
2024-08-09T12:51:47.99+00:00

I am developing ASP.NET application.

I have been using gridview for adding/editing/deleting records for most of the forms.

some of the gridview will have more than 1000 rows.

for temporary, i have used CSS overflow to freeze the records.

But, it will scroll the entire gridview (including header and rows.)

I want to freeze the gridview header column and then rows like excel freeze.

How to do this..? Is there any way to freeze gridview header except css overflow..?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,520 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,911 Reputation points Microsoft Vendor
    2024-08-12T03:41:48.1266667+00:00

    Hi @BeUnique,

    You can use jQuery DataTable plugin to fix the header.

    Demo:

     <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/fixedcolumns/4.1.0/js/dataTables.fixedColumns.min.js"></script>
    <link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
    <link href="https://cdn.datatables.net/fixedcolumns/4.1.0/css/fixedColumns.dataTables.min.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .auto-style1 { width: 600px; border-style: solid; border-width: 2px; }
        .dataTables_scrollHead { width: 600px !important; }
        .dataTables_scrollHeadInner { width: 600px !important; }
        .dataTables_scrollBody { width: 600px !important; }
    </style>
    <script type="text/javascript">
        $(function () {
            $('#gvCustomers tfoot tr').appendTo('#gvCustomers thead');
            $('#gvCustomers').removeAttr('width').DataTable({
                bFilter: true,
                bSort: true,
                scrollY: "150px",           
                scrollCollapse: true,
                paging: false,
                fixedColumns: false,
                orderCellsTop: true
            });
        });
    </script>
    

    Or use Responsive used with the DataTables FixedHeader extension.

    https://datatables.net/extensions/responsive/examples/column-control/fixedHeader.html

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread


2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,251 Reputation points
    2024-08-09T19:18:02.5133333+00:00

    See this thread

    https://stackoverflow.com/questions/8423768/freeze-the-top-row-for-an-html-table-only-fixed-table-header-scrolling

    but a 1000 rows is too many. You should use a JavaScript table with virtual scrolling. See the popular datatables

    https://datatables.net

    0 comments No comments

  2. Manish Kumar 0 Reputation points
    2024-08-14T01:15:23.02+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.