5,317,180 members and growing! (21,841 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner

How to Fixed GridView's Header and Footer when scrolling?

By fenglinzh

Using CSS, Javascript to Fixed Gridview's Header and Footer
Javascript, CSS, C# 2.0, C#, Windows, .NET, .NET 2.0, ASP.NET, VS2005, IE 6.0, IE 7, Visual Studio, IE, Dev

Posted: 24 Oct 2007
Updated: 30 Oct 2007
Views: 18,310
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 2.90 Rating: 2.90 out of 5
4 votes, 40.0%
1
0 votes, 0.0%
2
3 votes, 30.0%
3
2 votes, 20.0%
4
1 vote, 10.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Screenshot - gvDemo.jpg

Introduction

This article is to show you how to Fix GridView's Header and Footer in a simple code with CSS and JavaScript.

Background

Understand CSS and JavaScript

Using the code

  1. Use the below 2 Css Classes for GridView Header and footer
    .GVFixedHeader { font-weight:bold; background-color: Green; position:relative; 
                     top:expression(this.parentNode.parentNode.parentNode.scrollTop-1);}
    .GVFixedFooter { font-weight:bold; background-color: Green; position:relative;
                     bottom:expression(getScrollBottom(this.parentNode.parentNode.parentNode.parentNode));}
    
  2. Use the below JavaScript to compute Footer's Position
    <script language="javascript" type="text/javascript">
            function getScrollBottom(p_oElem)
            {
             return p_oElem.scrollHeight - p_oElem.scrollTop - p_oElem.clientHeight;
            }
    </script>
  3. Create GridView inside a Panle, Set Panle's property ScrollBars to "Auto", Gridview's HeaderStyle to "GVFixedHeader" and FooterStyle to "GVFixedFooter".
    <asp:Panel runat="server" ID="pnlContainer" ScrollBars="Auto" Height="150px" Width="400">
        <asp:GridView ShowFooter="True" runat="server" Width="96%" ID="gvDemo" AutoGenerateColumns="False">
        <HeaderStyle CssClass="GVFixedHeader" />
        <FooterStyle CssClass="GVFixedFooter" />
            <Columns>
                <asp:TemplateField HeaderText="C1">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("C1") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        C1 Footer Here
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="C2">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("C2") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        C2 Footer Here
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
       </asp:Panel>
  4. Code Behide: In the Page_Load function we Bind the data source to GridView.
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim dt As New DataTable
            dt.Columns.Add("C1")
            dt.Columns.Add("C2")
            Dim drRow As DataRow
            For i As Integer = 0 To 10
                drRow = dt.NewRow
                drRow(0) = "C1" & i
                drRow(1) = "C2" & i
                dt.Rows.Add(drRow)
            Next
            Me.gvDemo.DataSource = dt
            Me.gvDemo.DataBind()
        End Sub
  5. Run the Page, you will see that the Header and Footer is fixed when you scrolling.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

fenglinzh


My Website:
San pian Ye
HuaChao & June
Share Knowledges
Occupation: Web Developer
Location: Singapore Singapore

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAlternate method: no resize bugmemberkccougar6:04 6 Nov '07  
QuestionRe: Alternate method: no resize bugmemberpbansal1:40 31 Dec '07  
GeneralRe: Alternate method: no resize bugmemberkccougar4:17 31 Dec '07  
GeneralNice OnememberPrakash@kappsoft21:17 30 Oct '07  
GeneralMissing sourcememberchitty223:09 29 Oct '07  
AnswerRe: Missing sourcememberfenglinzh18:05 30 Oct '07  
GeneralOOPs!!!memberNigam SAMir19:26 25 Oct '07  
QuestionThis method only works well with low amount of rows in your gridmembertoprope983:59 30 Oct '07  
AnswerRe: This method only works well with low amount of rows in your gridmemberfenglinzh18:10 30 Oct '07  
GeneralRe: This method only works well with low amount of rows in your grid [modified]membertoprope988:37 31 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Oct 2007
Editor:
Copyright 2007 by fenglinzh
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project