A scrollable ASP.NET Gridview with a fixed header

No.of Views5283
Bookmarked0 times
Downloads 
Votes0
By  Paru   On  16 Feb 2010 02:02:41
Tag : ASP.NET , Grid Controls
A scrollable ASP.NET Gridview with a fixed header
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

ASP.NET Gridview has the provision of providing paging, set a particular pagesize etc.. But at times our clients are too adamant to demand for scrollable gridview with a steady header.Gridview as such doesn’t have a scrollable feature . But still a work around is possible. This is being discussed in this article that provides us a convenient scrollable grid with a steady header.

 STEP 1- Scrollable GridView

 Add the gridview into a asp:Panel control

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="gridpanel" ScrollBars="Auto">
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </asp:Panel>
    </form>
</body>
</html>

 

This Panel has a scroll bar feature so it tends to scroll the grid. But our problem is not yet solved. What is the next issue- Constant header.
The above code makes the whole panel content to scroll. But most of the application needs a constant header. Is it possible? This is explained in the next section. 

STEP 2- Stable header

This can be done in 2 ways

1. Add the css to the Panel

.fixedHeader
{
overflow: auto;
height: 145px;

}
table th
{

position: relative;

}

 

And the HTML code becomes
 

<asp:Panel ID="gridpanel" ScrollBars="Auto">
        <asp:GridView ID="GridView1" HeaderStyle-CssClass="fixedHeader" runat="server">
        </asp:GridView>
    </asp:Panel>

2. In the above way we have added style to the Panel as a whole.Again we can achieve stable header by adding header style for the grid.
This is the style:

.fixedHeader
{
background-color: aqua;
position: relative;
top: expression(this.offsetParent.scrollTop);
}


And the HTML becomes

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="gridpanel" ScrollBars="Auto">
        <asp:GridView ID="GridView1" HeaderStyle-CssClass="fixedHeader" runat="server">
        </asp:GridView>
    </asp:Panel>
    </form>
</body>
</html>

Conclusion

This article deals with a scrollable gridview that has a stable header.Though this is not possible as such as the gridview doesnot have a scrollable property it can be done by a work around. I hope it helps.Happy Coding.

 
Sign Up to vote for this article
 
About Author
 
Paru
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-31 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top