Thanigainathan Siranjeevi

Sharing my learning

  Home  |   Contact  |   Syndication    |   Login
  30 Posts | 0 Stories | 50 Comments | 0 Trackbacks

News

Twitter












Archives

ASP.Net

EntityFramework

Linq

Microsoft Free Ebook

Silverlight

Gridview is one of the good controls which enables us to produce Excel like output in the webforms.You can learn more about grid view in the following link GridView Documentation

We had a scenario where we are supposed to give freeze pane like feature for Gridview. We had a lengthy Grid with some 20 or 30 columns . We had paging for about 20 records per page. So following were the requirements.

1. Grid must have a Fixed header.
2. it should be scrollable horizontally.
3. it should be scrollable vertically.

I did a lot of search and could only get solutions that worked only where the columns are limited. So I did a small R&D and derived at a solution which i will be sharing now. Take for example a Timesheet application and it has a Grid. We are going to give fixed header feature for this Grid. The GridView will look like as fallow's.

<asp:GridView ID="grdTask" runat="server" GridLines  ="Both"    Width ="100%"  >
            <!-- Some column Definitions-->
</asp:GridView>

The first thing we will be doing is to move this Grid inside a Div tag.

<div  id = "AdjResultsDiv">
<asp:GridView ID="grdTask" runat="server" GridLines  ="Both"    Width ="100%"  >
            <!-- Some column Definitions-->
</asp:GridView>

</Div>

Lets say we have a StyleSheet file "Style.Css". Please add the following styles for the Div using ID based styles. You can also use Class based styles.

div#MyGrid {
width: 1080px;
height: 500px;
overflow: scroll;
position: relative;
}

The above code will make sure of the following things.

1. We can define the height and the width of the Div We are using.
2. The scrollbars that we may need or not using the Overflow property.
3. setting the Position to relative makes the Div relative to its parent.

The next thing will be the styles for the Grid. By default the grid will be rendered as a tabel with default Div tags as

<div>
<table ......

So when you add a Div Tag before the Grid it will be rendered as

<div id = "AdjResultsDiv">

<div>
<table ......


So our styles should be accordingly designed. I have given the Grid styles below.

div#MyGrid th
{  
top: expression(document.getElementById("AdjResultsDiv").scrollTop-2);
left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft);
position: relative;
z-index: 20;
}

Following are the functionalities.

1.Give the Top and Left coordinates of the TH tag which will be generated for the Header inside the Grid. We are using the expression tag to dynamically assign the values through JavaScript.
2.Place the Grid relatively inside the Div tag. If this property is not set the Header will go out of the Div Box.
3.Set the Z-Index of the Grid. The z-index property sets the stack order of an element. So based on the order they will be placed in front.

So add the following in the Style sheet :-

div#AdjResultsDiv {
width: 1080px;
height: 500px;
overflow: scroll;
position: relative;
}

div#AdjResultsDiv th
{  
top: expression(document.getElementById("AdjResultsDiv").scrollTop-2);
left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft);
position: relative;
z-index: 20;
}


And put the Grid inside the Div tag. That's all. You are now ready with the Fixed headers for the Grid.

You can find the Demo here.

Thanks,
Thani

posted on Tuesday, May 05, 2009 11:40 PM

Feedback

# re: GridView Fixed Header 5/8/2009 3:51 AM naga
excellent article

# re: GridView Fixed Header 5/10/2009 6:00 AM Thanigainathan Siranjeevi
Thanks Naga

# re: GridView Fixed Header 5/27/2009 9:23 AM kp
this works perfectly in IE. Any idea why it doesnt work in firefox?

# re: GridView Fixed Header 8/14/2009 7:05 AM Hiran
actually i' m searching fixed header working solution in firefox. could u share any idea if u have?

# re: GridView Fixed Header 8/28/2009 2:42 AM Naresh
Excellent.At last we found great solution

# re: GridView Fixed Header 8/31/2009 12:56 AM Thanigainathan
Thanks Naresh

# re: GridView Fixed Header 8/31/2009 7:33 AM Thanigainathan
Hi Guys ,

A small update. To make this working across all browsers just add the following jquery script at the page end.

<script type="text/javascript">
var obj = $('#AdjResultsDiv th');
var obj2 = $('#AdjResultsDiv th.lockedcol,td.lockedcol');
$('#AdjResultsDiv').scroll(sample);
function sample() {
obj2.css("left", ($('#AdjResultsDiv').scrollLeft()));
obj.css("top", ($('#AdjResultsDiv').scrollTop()));
};
</script>

Update the css as follows.

/* Div that holds the GridView control*/
div#AdjResultsDiv {
width: 950px;
height: 100px;
overflow: scroll;
position: relative;
}


/* Fix the table header for vertial scrolling*/
div#AdjResultsDiv th{
background-color:Gray;
top: 0;/*expression(document.getElementById("AdjResultsDiv").scrollTop-2);*/
position: relative;
z-index: 10;
border-collapse: collapse;
width:200px;
}


/* Fix the table header and free column for horizontal scrolling*/
div#AdjResultsDiv th.lockedcol,td.lockedcol
{
background-color:LightBlue;
position:relative;
left: 0;/*expression(document.getElementById("AdjResultsDiv").scrollLeft-2); */
z-index: 30;
border:1px solid white;
border-collapse: collapse;
}

/* Freeze column header adjusted for Vertical scrolling*/
div#AdjResultsDiv th.lockedcol{z-index: 40;}


Thanks,
Thani

# re: GridView Fixed Header 9/14/2009 6:39 AM amita
I m not able to see demo,i tried this but header wont be fixed when scrolling vertically

# re: GridView Fixed Header 9/30/2009 6:05 PM Adim (Brasil)
Dear,
I implemented the diamond tabalho and it is working very well. We needed to verify because it doesn't fasten the header for Browse Chrome.
Adim

# re: GridView Fixed Header 10/13/2009 10:17 PM reynie
Hello Thani,
I followed every step you specified on this article.
I dont know what went wrong, i can't get the results right.
The header is still not fixed.
I'm running it on IE8 browser.

# re: GridView Fixed Header 10/15/2009 3:00 AM suneel
hi , it is a very good artical

# re: GridView Fixed Header 10/21/2009 10:17 PM John Eric
You can check out this open-source CoolGridView project which extends ASP.NET GridView control. It supports fix headers, footer and pager; and scrollable content. You can also place it inside UpdatePanel with no issue. It works well in Internet Explorer 8.0.6001, Firefox 3.5.3 and Safari 4.0.2.

http://johnsobrepena.blogspot.com/2009/09/extending-aspnet-gridview-for-fixed.html

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: