Geeks With Blogs

@RiaGuru
  • RiaGuru Attending windows azure spring camp @Chennai and its cool about 334 days ago
  • RiaGuru I hosted my blog http://t.co/G1ZHfZB Its related to asp.net mvc,nhibernate. The blog is build on top of funnelweblog.com framework. about 675 days ago
  • RiaGuru I recently experienced the power of NHIbernate. A very powerful ORM ! about 792 days ago

Thanigainathan Siranjeevi Sharing my learning

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 5, 2009 11:40 PM | Back to top


Comments on this post: GridView Fixed Header

# re: GridView Fixed Header
Requesting Gravatar...
excellent article
Left by naga on May 08, 2009 3:51 AM

# re: GridView Fixed Header
Requesting Gravatar...
Thanks Naga
Left by Thanigainathan Siranjeevi on May 10, 2009 6:00 AM

# re: GridView Fixed Header
Requesting Gravatar...
this works perfectly in IE. Any idea why it doesnt work in firefox?
Left by kp on May 27, 2009 9:23 AM

# re: GridView Fixed Header
Requesting Gravatar...
actually i' m searching fixed header working solution in firefox. could u share any idea if u have?
Left by Hiran on Aug 14, 2009 7:05 AM

# re: GridView Fixed Header
Requesting Gravatar...
Excellent.At last we found great solution
Left by Naresh on Aug 28, 2009 2:42 AM

# re: GridView Fixed Header
Requesting Gravatar...
Thanks Naresh
Left by Thanigainathan on Aug 31, 2009 12:56 AM

# re: GridView Fixed Header
Requesting Gravatar...
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
Left by Thanigainathan on Aug 31, 2009 7:33 AM

# re: GridView Fixed Header
Requesting Gravatar...
I m not able to see demo,i tried this but header wont be fixed when scrolling vertically
Left by amita on Sep 14, 2009 6:39 AM

# re: GridView Fixed Header
Requesting Gravatar...
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
Left by Adim (Brasil) on Sep 30, 2009 6:05 PM

# re: GridView Fixed Header
Requesting Gravatar...
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.
Left by reynie on Oct 13, 2009 10:17 PM

# re: GridView Fixed Header
Requesting Gravatar...
hi , it is a very good artical
Left by suneel on Oct 15, 2009 3:00 AM

# re: GridView Fixed Header
Requesting Gravatar...
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
Left by John Eric on Oct 21, 2009 10:17 PM

# re: GridView Fixed Header
Requesting Gravatar...
hi, Thank you for sharing, its very cool.
I make it run but the issue i am facing is that other style sheet hover effects on my page are extreamly slow now. Any idea, why it made it so viscous and jerky?
Left by dexter on Dec 22, 2009 9:22 AM

# re: GridView Fixed Header
Requesting Gravatar...
Hi,

Its not possible becuase the GridView uses its own stylesheets. It wont affect others.

Thanks,
Thani
Left by Thanigainathan on Dec 22, 2009 10:49 PM

# re: GridView Fixed Header
Requesting Gravatar...
I could not get it working...
i pasted the same code written above but i do not get any scroll bars on my grid..plz help
Left by Mayank on Dec 23, 2009 8:18 AM

# re: GridView Fixed Header
Requesting Gravatar...
I tried the way you said its not working,and ID based style sheet is not working.and i tried using class,i want to knw i have to use class for gridview also,then this code will change?Please its urgent
Left by amita on Jan 05, 2010 1:05 AM

# re: GridView Fixed Header
Requesting Gravatar...
Can you tell me how to do with out using jquery,do u have any idea of it.m not supppose to use jquery.first part wokrs with IE but if i want use if both for IE and FF how to it,with out using jquery
Left by Amita on Jan 07, 2010 2:50 AM

# re: GridView Fixed Header
Requesting Gravatar...
The thing is like you have adjust the left and top of the div according to the div scroll.

Thanks,
Thani
Left by Thanigainathan on Jan 07, 2010 7:14 AM

# Still can't get it to work in Firefox
Requesting Gravatar...
Works great in IE but I still can't get it to work in Firefox. When I add the above fix with the Javascript, it then quits working in IE?
Left by Mark Liberatore on Jan 29, 2010 1:22 PM

# re: GridView Fixed Header
Requesting Gravatar...
Thank you for this..i searched for awhile on how to do this and everyone elses header overlaps the <div>.

Is there a way I can freeze the first column in my gird without the column overlaping the height of my grid? On my page, the user has the ability to change the pagesize of the grid. Any help would be great
Left by captain on Feb 17, 2010 2:29 PM

# re: GridView Fixed Header
Requesting Gravatar...
SOrry I meant to say the first row
Left by Captain on Feb 17, 2010 2:31 PM

# re: GridView Fixed Header
Requesting Gravatar...
i used your coding .it works.also i want fixed margin with fixed header.now i get fixed headar with the help of you.can you please help me to get the codings for fixed margin also

thank you
Left by jicky on Feb 17, 2010 11:37 PM

# re: GridView Fixed Header
Requesting Gravatar...
So can u pls tell me how can i adjust the top of div so tht it could work for crossbrowser for fixed header
Left by amita on Mar 21, 2010 11:10 PM

# re: GridView Fixed Header
Requesting Gravatar...
Thanks for sharing this tip! I've been trying to do this, but was unsuccessful. you surely are a lot of help!
Left by combi boiler on May 09, 2010 4:47 PM

# re: GridView Fixed Header
Requesting Gravatar...
Hey Thani,
Great efforts on good subject. demo is not available.
I tryied unable to compile, it says object not found for[div#AdjResultsDiv th].

I am not conform wherether i code th proparly. can u plz do a favour, please upload sample code.

Regards,
Ashish
Left by Ashish Dhanorkar on Sep 15, 2010 1:01 AM

# re: GridView Fixed Header
Requesting Gravatar...
expression(document.getElementById("AdjResultsDiv").scrollTop-2);
is useful only in IE not in any other browser.
Kindly suggest me for Safari,Mozilla and Chrome etc.
Left by Pravin Patil on Sep 15, 2010 5:42 AM

# re: GridView Fixed Header
Requesting Gravatar...
Hi Thani,

Can you please send the source code or please upload sample code

Thank
Pradeep
Left by Pradeep on Oct 01, 2010 6:34 AM

# re: GridView Fixed Header
Requesting Gravatar...
Hii,
Thank you very much for the solution. It works fine. But if only one row needs to display then the grid won't be able to display the single row. it's been hided by the header row.

please give me your comments/suggestions.

thanks
Left by Thiyagarajan on Oct 06, 2010 2:31 AM

# re: GridView Fixed Header
Requesting Gravatar...
Please help me using css exprssion i fixed header in html table but it is not working very urgent help me.......
Left by karthik.T on Oct 25, 2010 6:25 PM

# re: GridView Fixed Header
Requesting Gravatar...
<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>

Using this script still have problem in firefox please give your advice or send sample code or demo
Left by KARTHIK on Oct 28, 2010 9:22 AM

# re: GridView Fixed Header
Requesting Gravatar...
This JQuery works very well in IE but when it comes to firefox or safari its not working. While scrolling all content is scrolling also including fixed column and headers.
Left by Anney on Nov 04, 2010 1:02 AM

# re: GridView Fixed Header
Requesting Gravatar...
Can you please give the script that you have tried ?
Left by Thanigainathan on Nov 06, 2010 12:44 PM

# re: GridView Fixed Header
Requesting Gravatar...
This does not work at all. Your example in the page does not clearly state why div#MyGrid works as an id. Also your Demo link is broken
Left by Craig Burkett on Nov 20, 2010 1:45 PM

# re: GridView Fixed Header
Requesting Gravatar...
i above your jquery example work IE fine but it is not working firefox please give example for that one.
Left by Karthik.T on Dec 29, 2010 4:36 AM

# re: GridView Fixed Header
Requesting Gravatar...
In firefox 3.6 it's working but in IE8 it's not working.
Left by Pino Ruffilli on Jan 11, 2011 10:44 AM

# re: GridView Fixed Header
Requesting Gravatar...
Awesome! Thanks! Saved me a great deal of time and effort.
Left by FlyGuy on Feb 03, 2011 1:41 PM

# re: GridView Fixed Header
Requesting Gravatar...
sir ,

Fixed header is not suport in mirefox
plz help me
Left by laxman on Mar 08, 2011 11:21 PM

# re: GridView Fixed Header
Requesting Gravatar...
thanks for this code.. coz it helps me lot.. but this is working when there is no vertical scroll bar in page...but when the page has scroll bar then header is not fix ..it will move again... please suggest me something that will help me in any case .
Left by sushma nibrad on Mar 12, 2011 12:20 AM

# re: GridView Fixed Header
Requesting Gravatar...
After long battle i find a smart guy.hmmm..only thing is scrolling is little slow
Left by sachithanandham on Mar 21, 2011 4:59 AM

# re: GridView Fixed Header
Requesting Gravatar...
u r code is not working in .. there is showing some error in style top:expression(document.getElementById("AdjResultsDiv").scrollTop-2);
left:expression(parentNode.parentNode.parentNode.parentNode.scrollLeft);

error expression is not valid for left,top property
Left by Sathi on Apr 12, 2011 6:53 AM

# re: GridView Fixed Header
Requesting Gravatar...
this very simple and powerful
Thank you very much
Left by maneesh on Apr 15, 2011 7:03 AM

# re: GridView Fixed Header
Requesting Gravatar...
its not working in firefox,
Left by mahady on May 28, 2011 11:08 PM

# re: GridView Fixed Header
Requesting Gravatar...
Dear Sir,

Can u send me code for this . i am struggling for last 6 months and tried many solutions.

Thanks in advance.

Left by Vipin Bhatia on Jul 10, 2011 4:25 AM

# re: GridView Fixed Header
Requesting Gravatar...
Thanks for this code.
Works fine in IE 9.
Left by Brena Monteiro on Aug 18, 2011 12:54 AM

# re: GridView Fixed Header
Requesting Gravatar...
Can U send me the code for Freezeing gridview Header in Ie9
Left by chowdary on Sep 15, 2011 7:36 PM

# re: GridView Fixed Header
Requesting Gravatar...
Hi, it seems like this code is working on some and not on others. Unfortunately, it is also not working for me. Is it like I need to update my jquery script jquery-1.4.1.min.js to something latest.

Whenever I run my page, i received the message "Microsoft JScript runtime error: Object expected" on this line "var obj = $('#AdjResultsDiv th');"

Any suggestion would be highly appreciated!

Thanks,
Fred
Left by Fred on Sep 16, 2011 4:38 AM

# re: GridView Fixed Header
Requesting Gravatar...
hi thanigainathan,
i'm using a telerik radgrid control which contains more than 200 records,can u suggest how to fix the header row of the grid. please its very urgent.
Left by raji on Sep 21, 2011 2:03 AM

# re: GridView Fixed Header
Requesting Gravatar...
Hi Raji,

Telerik Rad controls uses lot div panels for generating a grid. So you have to find the option which will be already available in the telerik grid to enable that or you have to get in touch with telerik customer support.
Left by Thani on Sep 22, 2011 11:55 PM

# re: GridView Fixed Header
Requesting Gravatar...
ok thani.... thanks for ur suggestion... will try it. thank u so much
Left by raji on Oct 09, 2011 10:30 PM

# re: GridView Fixed Header
Requesting Gravatar...
thank you very much... it helped me alot...
Left by naabster on Nov 07, 2011 10:49 PM

# re: GridView Fixed Header
Requesting Gravatar...
Hi there,
I have tried this but no luck. I am using IE 9. I wonder if any of you have been able to make it work in IE 9 and would like to share the solution.

Regards,
Luvy
Left by Luvy Gonzalez on Nov 24, 2011 10:53 AM

# re: GridView Fixed Header
Requesting Gravatar...

Hi guys,
I got it to work by using the JQuery fix posted by KARTHIK. Thanks!

Luvy Gonzalez
Left by Luvy Gonzalez on Nov 25, 2011 7:05 AM

# re: GridView Fixed Header
Requesting Gravatar...
Superb solution :) really great
Left by Dev on Nov 30, 2011 9:04 PM

# re: GridView Fixed Header
Requesting Gravatar...
sir , the code i was searching for last 2 hours was found in your blog , i m now implementing this code into my blog ikonwebdesigns.com thanks sir.
Left by surinder on Dec 04, 2011 8:39 PM

# re: GridView Fixed Header
Requesting Gravatar...
Hi All,

I want to implement same in my application. I have applied same coding in my logic, but not working.

My Grid control is under Update Panel-->table--> td-->panel-->div-->Grid.

Is this cause it is not working?

Please help me.
Left by Mallik on Jun 07, 2012 9:00 PM

# re: GridView Fixed Header
Requesting Gravatar...
I have followed the same steps but still couldn't fix the header. My Grid is under Panel

<asp:Panel ID="PanelTest" runat="server">
<asp:GridView ID="GridViewTest" runat="server">
<Columns>
...
</Columns>
</asp:GridView>
</asp:Panel>
Left by Viswanathan on Jul 16, 2012 7:45 PM

Your comment:
 (will show your gravatar)
 


Copyright © thanigai | Powered by: GeeksWithBlogs.net | Join free