Freeze header of scrollable datagrid

本文介绍了一种简单的方法来冻结DataGrid的头部和列,提高滚动时的用户体验。通过定义CSS类并利用CSS表达式,可在IE5及以上浏览器中轻松实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code Snippet: Freezing Datagrid Header/Columns Made Easy
First posted on 11/10/2004, Last Updated on 5/28/2005 by Richard Xin

Demo: (Freeze first 2 columns & header)
ProductIDProductNameSupplierIDCategoryIDQuantityPerUnitUnitPriceUnitsInStockUnitsOnOrderReorderLevelDiscontinued
17Alice Mutton7620 - 1 kg tins39.0000000True
3Aniseed Syrup1212 - 550 ml bottles10.0000137025False
18Carnarvon Tigers7816 kg pkg.62.50004200False
1Chai1aaa1110 boxes x 20 bags18.000039010False
2Chang1124 - 12 oz bottles19.0000174025False
39Chartreuse verte181750 cc per bottle18.00006905False
4Chef Anton's Cajun Seasoning2248 - 6 oz jars22.00005300False
5Chef Anton's Gumbo Mix2236 boxes21.3500000True
38C?te de Blaye18112 - 75 cl bottles263.500017015False
33Geitost154500 g2.5000112020False
15Genen Shouyu6224 - 250 ml bottles15.50003905False
31Gorgonzola Telino14412 - 100 g pkgs12.500007020False
6Grandma's Boysenberry Spread3212 - 8 oz jars25.0000120025False
37Gravad lax17812 - 500 g pkgs.26.0000115025False
24Guaraná Fantástica10112 - 355 ml cans4.50002000True
26Gumb?r Gummib?rchen113100 - 250 g bags31.23001500False
22Gustaf's Kn?ckebr?d9524 - 500 g pkgs.21.0000104025False
10Ikura4812 - 200 ml jars31.00003100False
36Inlagd Sill17824 - 250 g jars19.0000112020False
13Konbu682 kg box6.00002405False
32Mascarpone Fabioli14424 - 200 g pkgs.32.000094025False
9Mishi Kobe Niku4618 - 500 g pkgs.97.00002900True
30Nord-Ost Matjeshering13810 - 200 g glasses25.890010015False
8Northwoods Cranberry Sauce3212 - 12 oz jars40.0000600False
25NuNuCa Nu?-Nougat-Creme11320 - 450 g glasses14.000076030False
16Pavlova7332 - 500 g boxes17.450029010False
11Queso Cabrales541 kg pkg.21.0000223030False
12Queso Manchego La Pastora5410 - 500 g pkgs.38.00008600False
28R?ssle Sauerkraut12725 - 825 g cans45.60002600True
34Sasquatch Ale16124 - 12 oz bottles14.0000111015False
27Schoggi Schokolade113100 - 100 g pieces43.900049030False
20Sir Rodney's Marmalade8330 gift boxes81.00004000False
21Sir Rodney's Scones8324 pkgs. x 4 pieces10.00003405False
35Steeleye Stout16124 - 12 oz bottles18.000020015False
19Teatime Chocolate Biscuits8310 boxes x 12 pieces9.20002505False
29Thüringer Rostbratwurst12650 bags x 30 sausgs.123.7900000True
14Tofu6740 - 100 g pkgs.23.25003500False
23Tunnbr?d9512 - 250 g pkgs.9.000061025False
7Uncle Bob's Organic Dried Pears3712 - 1 lb pkgs.30.000015010False

 

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" ...></ HeaderStyle>  
 

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值