Freezing Datagrid Header/Columns Made Easy

博客介绍了冻结列的实现方法,先在样式表中定义.FreezingCol 类,再将列的 cssClass 赋值为 FreezingCol,若动态创建列可在 ItemCreated 事件运行时赋值。此方案仅适用于 IE5 及以上,跨浏览器需用 JavaScript。

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

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!

转载于:https://www.cnblogs.com/rengy/archive/2005/09/30/247093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值