Freezing DataGrid Header/columns made easy

Source: Internet
Author: User
You have already known how to make you long DataGrid scrollable, however, when you scroll the grid, the header of the DataGrid scrolls away. to make scrollable DataGrid more user friendly, you may want to "freeze" the header while scrolling, and sometimes you may need to "freeze" your columns like Excel spreedsheet. there are some third party components around to help you to implement these, and I believe those components used JavaScript heavily, it will take lots of time to develop such component if you decide to do it your own. my solution is pretty easy with only couple lines of code, the con is that it only works with IE 5 & up, becase I used CSS expression Which is used to assign a javascript expression to a CSS property, and it was first introduced in IE 5.
Freeze header:
1. Define class. Freezing in stylesheet:
. Freezing
{
Position: relative;
Top: expression (this. offsetparent. scrolltop );
Z-index: 10;
}

2. Assign DataGrid header's cssclass to freezing

<Headerstyle... cssclass = "freezing"...>

3. You are done!

Freeze columns:
1. Define class. freezingcol in stylesheet:

. Freezingcol
{
Left: expression (document. getelementbyid ("freezingdiv"). scrollleft );
/* Freezingdiv is the name of the DIV to make your DataGrid scrollable */
Position: relative;
Z-index: 1;
}

2. Assign columns 'cssclass to freezingcol
If you have templatecolumn, you can simply assign cssclass of the column to freezingcol. If you create the columns dynamically, you can assign the cssclass at runtime within itemcreated event. For example:

E. Item. cells (0). cssclass = "freezingcol"
E. Item. cells (1). cssclass = "freezingcol"

3. You are done! Please keep in mind this solution only works with ie5 & up. To make cross browser solution, you need to use JavaScript which is out of scope of this article.

Happy programming!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.