一 和鼠标移动时改变背景颜色一样,采用样式来固定行和列
/*固定行*/
.yy_sgv_fixRow
{
position: relative; top: expression(this.offsetParent.scrollTop - 1);background-color:Green;
//根据常规流定位对象,然后同Top和Left特性进行偏移
//offsetParent特性,检索对象顶部和左偏移的容器对象的引用
//ScrollTop特性,设置或检索对象顶部和窗口中当前可视内容的最顶部位置之间的距离(滑块的位置)
//IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和Javas cript表达式关联起来,
//这里的CSS属性可以是元素固有的属性,也可以是自定义属性。就是说CSS属性后面可以是一段
//Javas cript表达式,CSS属性的值等于Javas cript表达式计算的结果。
}
/*固定列*/
.yy_sgv_fixCol
{
position: relative; left: expression(this.offsetParent.scrollLeft - 1);
//ScrollLeft特性,设置或检索对象的左边缘和窗口中当前可视内容的最左边位置之间的距离
}
/*高优先级的固定*/
.yy_sgv_fixHigh
{
z-index: 9999;
}
/*低优先级的固定*/
.yy_sgv_fixLow
{
z-index: 1000;
}
ScrollTop:
解释:
<div style="width:200px;height:200px;background-color:#999999;overflow:auto;" id="外层元素">
<div style="width:100px;height:300px;background-color:#FFFF00;" id="内层元素">
这些文字显示在内层元素中。
</div>
</div>
内层元素的高度值300px > 外层元素的高度值200px,因此“外层元素的内容”(也就是“内层元素”)无法完全显示,
而外层元素把overflow设置为auto,因此外层元素的右侧会出现一个上下方向的滑动条
初始状态下,“内层元素的上边界”和“外层元素的上边界”重合,没有任何内容超过“外层元素的上边界”,
此时外层元素的scrollTop属性的值为0。
当向下拖动滚动条时,超过“外层元素的上边界”的内容会逐渐增多,外层元素的scrollTop值就等于这些超出的部分。
当拖动滚动条到最底部时,“内层元素的下边界”和“外层元素的下边界”重合,超过“外层元素的上边界”
的内容的高度=300px-200px=100px,这也就是此时的外层元素的scrollTop值。
二 自定义了委托和事件及事件的处理函数来处理固定行和固定列
public delegate void RowDataBoundCellHandler(object sender, GridViewTableCell gvtc);
private static readonly object rowDataBoundCellEventKey = new object();
/// <summary>
/// RowDataBound事件中的所有单元格
/// </summary>
[Category("扩展")]
public event RowDataBoundCellHandler RowDataBoundCell
{
add { Events.AddHandler(rowDataBoundCellEventKey, value); }
remove { Events.RemoveHandler(rowDataBoundCellEventKey, value); }
}
/// <summary>
/// 触发RowDataBoundCell事件
/// </summary>
/// <param name="gvtc"></param>
protected virtual void OnRowDataBoundCell(GridViewTableCell gvtc)
{
RowDataBoundCellHandler handler = Events[rowDataBoundCellEventKey] as RowDataBoundCellHandler;
if (handler != null)
{
handler(this, gvtc);
}
}
this._sgv.RowDataBoundCell += new MyGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
void _sgv_RowDataBoundCell(object sender, GridViewTableCell gvtc)
在这个处理函数中程序根据新添加的复杂属性FixRowColumn的各项来对相应的表项添加属性,
这里的这个属性也就是样式
Helper.Common.SetAttribute(tc, "class", "yy_sgv_fixRow", AttributeValuePosition.Last, ' ');
Helper.Common.SetAttribute(tc, "class", "yy_sgv_fixCol", AttributeValuePosition.Last, ' ');
三 通过重写 protected override void OnRowDataBound(GridViewRowEventArgs e) 来激活自定义的委托和事件
//绑定数据时引发OnRowDataBoundCell事件,
//处理固定行和固定列
for (int i = 0; i < e.Row.Cells.Count; i++)
{
GridViewTableCell gvtc = new GridViewTableCell(e.Row.Cells[i], i, e.Row.RowType, e.Row.RowState);
OnRowDataBoundCell(gvtc);
}