Click here to Skip to main content
Click here to Skip to main content

ASP.NET DataGrid Header Slider Control

By , 21 Mar 2004
 

Sample Image - GridSlideHeader.jpg

Introduction

Sometimes when tables display lots of data, it's difficult and cumbersome for the users to scroll back and forth to see which header corresponds to which column. I've developed a simple add-on control that will work with existing table type controls such as DataGrids and DataLists. The GridSlideHeader is an ASP.NET custom control that implements JavaScript (and CSS) that will "dock" the table's header when the header is scrolled out of the page.

For a live demo, check this link out.

Using the server control

The GridSlideHeader server control is a very simple add-on control and can be added to existing DataGrids and DataLists.

Simply call the GridSlideHeader control and set the DataGridID property that corresponds to the DataGrid/DataList ID. That's it!

Example:

<form id="Form1" method="post" runat="server">
   <asp:datagrid id="DataGridReport1" runat="server" 
         AutoGenerateColumns="False" BorderColor="#000066">
   <EditItemStyle ForeColor="Black" BackColor="Yellow"></EditItemStyle>
   <AlternatingItemStyle Font-Size="X-Small" 
         BackColor="Gainsboro"></AlternatingItemStyle>
   <ItemStyle Font-Size="X-Small" Font-Names="Verdana"></ItemStyle>
   <HeaderStyle Font-Bold="True" BackColor="#D7D7D7"></HeaderStyle>
   <Columns>
      <asp:BoundColumn DataField="Title_ID" 
           HeaderText="Title_ID" SortExpression="Title_ID" />
      <asp:BoundColumn DataField="Title" HeaderText="Title" 
           SortExpression="Title" />
      <asp:BoundColumn DataField="Price" HeaderText="Price" 
           SortExpression="Price" />
      <asp:BoundColumn DataField="Publication" 
           HeaderText="Publication" SortExpression="Publication" />
      <asp:BoundColumn DataField="Type" HeaderText="Type" 
           SortExpression="Type" />
      <asp:BoundColumn DataField="Authors" HeaderText="Authors" 
           SortExpression="Authors"></asp:BoundColumn>
   </Columns>
   </asp:datagrid>
   <cc1:GridSlideHeader id="GridSlideHeader1" 
           runat="server" DataGridID="DataGridReport1" />
</form>

Conclusion

This server control was quickly written and has much room for improvement. I didn't have time to implement the designer control stuff, so it may not look great in design mode. When implementing the JavaScript, instead of setting the TR positions, the control sets the TD positions of each header item. This was done to maintain the header borders. Lastly, I've been told this control is only compatible on IE platforms. Theoretically, it should be compatible on any browser that supports CSS positioning, but this is untested :P. Enjoy and let me know what bugs you find~...

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

Tony Truong
Architect Frontline Direct Inc., Adconion
United States United States
Member
Tony Truong graduated from UCLA in Spring of 2001 and starting worked at Symantec Corporation as a Software Engineer. After a few years of developing various features for Norton SystemWorks, Tony moved to San Diego. He is currently writing database applications using ASP.NET and C# with the .NET Framework. Tony specializes in tara-byte databases with emphasis on high availability, optimization, and complex entity modeling.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generaldose it work with sharepoint webaprt programmin???? [modified]membereyadAdwan3 Aug '06 - 3:24 
i was tried it with a webpart project that contains a datagrid but it doesn't work. any solution about this....
 
thanks
 

-- modified at 9:28 Thursday 3rd August, 2006
 
EyAd

GeneralError Creating ControlmemberCarlesP3 Jul '06 - 10:51 
Hi there, I'm using your control, but I'm always getting this error "Error Creating Control GridSlideHeader1". Any assistance is welcome. Thanks for your time.
GeneralDos it work with DatagridViewmemberOmar20035 Mar '06 - 19:12 
Hi
Thank for this control
Dos it work with Datagridview in VS.2005
ThyanksSmile | :)
 

QuestionUmm?sussARandomUserWithYourBestInterests23 Feb '05 - 8:46 
This can be done with a style sheet reference, which may be why you are having problems outside of IE, as the call is IE only.
 
Set UseAccessibleHeaders to on this will render the header tags as TH then add a reference like this in your style sheet.
 
th.locked {
color: #0000FF;
z-index: 99;
left: expression(document.getElementById("MyDivNameHere").scrollLeft);
}
 
expressions like this are only supported in IE though
 
/shrug
GeneralVery GoodmemberOmar200315 Feb '05 - 19:08 
Thank u for this very usefull control
 
is there a way to use it vertically
GeneralAdditionsmemberFattyMonkey20 May '04 - 4:21 
Thanks Tony for the control.
 
I've added a zIndex property because the slider goes behind divs and tables when used with HierarGrid.
 
Also added designer show "GSH for GridId" on design page.
 
If you want a zip of the source please email me. I can't find your address.
 
Dave.
dbrookes@awtxglobal.com
GeneralWorks like a ChampmemberChuckle17 May '04 - 19:40 
hope you dont mind me making a few additions...Big Grin | :-D
GeneralRe: Works like a ChampmemberTony Truong17 May '04 - 20:03 
Not a prob... Send me a link to a demo of any new features you add on if you get a chance.
GeneralGood for simple layouts, bails *hard* on more complex pagessussyourlordjesus24 Apr '04 - 14:45 
Smile | :) Nice and simple and works a treat on most pages.
Managed to kill vs.net in design mode though, with a page containing 6 datagrids - only the bottom one had your control associated with it. After adding the control tag in html and switching back to design view, memory and cpu usage went through the roof - had to kill the process@100% cpu and 800MB virtual memory!Cry | :((
 
The page is kinda complex, and I've yet to narrow down what causes this exactly, but will keep you posted- will try gradually removing sections of the page until it no longer dies etc, etc.
GeneralAdding to toolbox in VS.Netmemberlillrune15 Apr '04 - 3:55 
That seems to be just what I'm looking for. I ran into problems when trying to add GorthControls.dll in the toolbox. It "could not be loaded. Check that any dependencies the file requires are installed". Any idea what this means?
Generalawesomemembersyusman4 Apr '04 - 17:19 
great job man!! Smile | :)
QuestionDosn't work with div tags?sussCalle Manthey30 Mar '04 - 0:25 
It seems not working when the DataGrid is in a div tag.
Is this right?
 
Calle
AnswerRe: Dosn't work with div tags?sussAnonymous25 May '04 - 16:56 
Tested in my app and it does not work within a "
" tag. Any fix for this?
GeneralRe: Dosn't work with div tags?memberTony Truong25 May '04 - 19:08 
I wish I had the time to make this bug fix but I can quickly explain why it's not working. The current version assumes the table tag is the outer most node within the form tag. You'll have to modify the JavaScript to dig through nested tags to find the datagrid ID (tableID)
GeneralDoesn't work with MozillasussAnonymous27 Mar '04 - 2:13 
Mozilla and Firefox don't work.
GeneralRe: Doesn't work with MozillamemberTony Truong27 Mar '04 - 10:17 
Oh well.. Who uses anything besides IE :P
GeneralRe: Doesn't work with MozillasussAnonymous28 Mar '04 - 23:36 
Mad | :mad:
 
You should learn more about w3c standards before posting a single-browser useless control... Dead | X|
GeneralRe: Doesn't work with MozillamemberTony Truong29 Mar '04 - 6:41 
1. The only programmers that would use this control is someone working on an internal company site, at which point IE is 99.9% of audience. You unix fokes have to deal with compatibilty issues all the time so this shouldn't be anything new.
 
2. If you read the threads on this article, you'll notice that some people have found the control extremely useful since they said that's exactly what they were looking for.
 
3. I didn't write the JavaScript/DHMLT portion of the code. It was taken from one of DynamicDrive's articles and is documented as such.
GeneralRe: Doesn't work with Mozillamembersmortensen24 Apr '04 - 14:50 
Yup. The world agrees...
http://www.w3schools.com/browsers/browsers_stats.asp
GeneralRe-sortingsussAnonymous26 Mar '04 - 4:01 
Tried the demo page. Attempting to re-sort throws an error. Does this control allow re-sorting of columns?

GeneralRe: Re-sortingmemberTony Truong26 Mar '04 - 4:23 
Yes sorting should work fine... What error screen are you seeing? In the demo, I'm using a session to hold the dataset that is read during pageload... If you wait till the session expires, than you may see a object null error.
GeneralRe: Re-sortingsussAnonymous2 Apr '04 - 3:21 
I'm not the original poster of this thread, but some co-workers and I have tried the demo, and we're all getting the same error as soon as you open the demo and click on a header. Here's the details, not that there's a ton of information since we already know it happens when you Sort:
 
"Object reference not set to an instance of an object.
Description: An unhandled exception occurred
 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
 
Source Error:
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
 
Stack Trace:
 

[NullReferenceException: Object reference not set to an instance of an object.]
GridSlideHeaderDemo.WebForm1.OnSort(Object source, DataGridSortCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnSortCommand(DataGridSortCommandEventArgs e) +110
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +422
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1277"
GeneralRe: Re-sortingmemberTony Truong2 Apr '04 - 16:23 
I've made some adjustments to the OnSort function (not to the control) that will avoid session object problems. Try it again and let me know Confused | :confused:
GeneralPerfecto!sussPhutile25 Mar '04 - 18:37 
Beautiful...just what i've been looking for! This allows me to avoid those EXPENSIVE datagrid controls and focus on the datagrid control that i'm extremely familiar with now. Thank you very much. OMG | :OMG:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 22 Mar 2004
Article Copyright 2004 by Tony Truong
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid