Click here to Skip to main content
13,052,494 members (67,749 online)
Click here to Skip to main content


14 bookmarked
Posted 25 Jun 2009

Fixing a Header in Datagrid

, , 25 Jun 2009
A simple way to fix the Datagrid header using JavaScript and CSS
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FixedHeaderDatagrid.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>Scrolling Header for Datagrid</title>
    <script type="text/javascript" language="javascript">
        function setonScroll(divObj,DgID)  // call this in onscroll event in div
            var datagrid = document.getElementById(DgID);
            var HeaderCells = datagrid.getElementsByTagName('th');
            var HeaderRow;
            if(HeaderCells == null || HeaderCells.length == 0)     // check wether any tableheader cells present in given datagrid
                var AllRows = datagrid.getElementsByTagName('tr');
                HeaderRow = AllRows[0];        
                HeaderRow = HeaderCells[0].parentNode;        
            var DivsTopPosition = parseInt(divObj.scrollTop);
       = 'absolute';
       = (parseInt(DivsTopPosition)).toString() + 'px';
                divObj.scrollTop = 0;
       = 'relative';
       = '0';
    <form id="form1" runat="server">
    <div id="Div" runat="server" style="position:relative; top:0px; left:0px; height:500px; width:250px; overflow:auto">
    <asp:DataGrid ID="dgCheckScrollingHeader" runat="server" Width="410px" AutoGenerateColumns="False" CellSpacing="1"  CellPadding="0" ForeColor="#333333" GridLines="vertical">
            <asp:TemplateColumn HeaderText="Column 1">
                    <asp:Label ID="lblCol1Data" Width="130px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Col1Data") %>' ></asp:Label>
                <HeaderStyle Width="150px" />
                <ItemStyle Width="150px" />
            <asp:TemplateColumn HeaderText="Column 2">
                    <asp:Label ID="lblCol2Data" Width="170px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Col2Data") %>' ></asp:Label>
                <HeaderStyle Width="180px" />
                <ItemStyle Width="180px" />
            <asp:TemplateColumn HeaderText="Column 3">
                    <asp:Label ID="lblCol3Data" Width="70px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Col3Data") %>' ></asp:Label>
                <HeaderStyle Width="80px" />
                <ItemStyle Width="80px" />
        <AlternatingItemStyle BackColor="White" />
        <ItemStyle BackColor="#EFF3FB" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" Height="25px" Font-Size="15px" ForeColor="White" HorizontalAlign="Center" Wrap="false" />

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Authors

Web Developer
India India
No Biography provided

Web Developer
India India
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 26 Jun 2009
Article Copyright 2009 by S.Rajeshwar, raj8696
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid