扩展GridView控件(5) - 固定指定行、指定列

GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[源码下载]


扩展GridView控件(5) - 固定指定行、指定列


作者: webabcd
 
InBlock.gif /*正式版的实现 开始*/
 
介绍
扩展GridView控件:
固定指定行、指定列,根据RowType固定行,根据RowState固定行

使用方法(设置FixRowColumn复合属性): 
FixRowType - 需要固定的行的RowType(用逗号“,”分隔)
FixRowState - 需要固定的行的RowState(用逗号“,”分隔)
FixRows - 需要固定的行的索引(用逗号“,”分隔)
FixColumns - 需要固定的列的索引(用逗号“,”分隔)
TableWidth - 表格的宽度
TableHeight - 表格的高度


关键代码
css
/*固定行*/ 
.yy_sgv_fixRow 
{  
        position
:  relative;  top:  expression(this.offsetParent.scrollTop - 1)
}  
/*固定列*/ 
.yy_sgv_fixCol 
{  
        position
:  relative;  left:  expression(this.offsetParent.scrollLeft - 1)
}  
/*高优先级的固定*/ 
.yy_sgv_fixHigh 
{  
        z-index
:  9999
}  
/*低优先级的固定*/ 
.yy_sgv_fixLow 
{  
        z-index
:  1000
}
 
c#
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI; 
InBlock.gif 
InBlock.gif namespace YYControls.SmartGridViewFunction 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 扩展功能:固定指定行、指定列 
InBlock.gif         /// </summary> 
InBlock.gif         public  class FixRowColumnFunction : ExtendFunction 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 构造函数 
InBlock.gif                 /// </summary> 
InBlock.gif                 public FixRowColumnFunction() 
InBlock.gif                        :  base() 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 构造函数 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="sgv">SmartGridView对象</param> 
InBlock.gif                 public FixRowColumnFunction(SmartGridView sgv) 
InBlock.gif                        :  base(sgv) 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 扩展功能的实现 
InBlock.gif                 /// </summary> 
InBlock.gif                 protected  override  void Execute() 
InBlock.gif                { 
InBlock.gif                         this._sgv.RowDataBoundCell +=  new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell); 
InBlock.gif                         this._sgv.RenderBegin +=  new SmartGridView.RenderBeginHandler(_sgv_RenderBegin); 
InBlock.gif                         this._sgv.RenderEnd +=  new SmartGridView.RenderEndHandler(_sgv_RenderEnd); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// SmartGridView的RowDataBoundCell事件 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="sender"></param> 
InBlock.gif                 /// <param name="gvtc"></param> 
InBlock.gif                 void _sgv_RowDataBoundCell( object sender, GridViewTableCell gvtc) 
InBlock.gif                { 
InBlock.gif                        TableCell tc = gvtc.TableCell; 
InBlock.gif                        GridViewRow gvr = (GridViewRow)tc.Parent; 
InBlock.gif 
InBlock.gif                         int i = 0;  // 0-既不固定行也不固定列;1-固定行或固定列;2-既固定行也固定列 
InBlock.gif                         // 固定行 
InBlock.gif                         if    
InBlock.gif                        ( 
InBlock.gif                                ( 
InBlock.gif                                        !String.IsNullOrEmpty( this._sgv.FixRowColumn.FixRows)    
InBlock.gif                                        &&    
InBlock.gif                                        Array.Exists( this._sgv.FixRowColumn.FixRows.Split(','),  delegate( string s) {  return s == gvr.RowIndex.ToString(); }) 
InBlock.gif                                ) 
InBlock.gif                                ||    
InBlock.gif                                ( 
InBlock.gif                                        !String.IsNullOrEmpty( this._sgv.FixRowColumn.FixRowType)    
InBlock.gif                                        &&    
InBlock.gif                                        Array.Exists( this._sgv.FixRowColumn.FixRowType.Split(','),  delegate( string s) {  return s == gvr.RowType.ToString(); }) 
InBlock.gif                                ) 
InBlock.gif                                ||    
InBlock.gif                                ( 
InBlock.gif                                        !String.IsNullOrEmpty( this._sgv.FixRowColumn.FixRowState)    
InBlock.gif                                        &&    
InBlock.gif                                        Array.Exists( this._sgv.FixRowColumn.FixRowState.Split(','),  delegate( string s) {  return s == gvr.RowState.ToString(); }) 
InBlock.gif                                ) 
InBlock.gif                        ) 
InBlock.gif                        { 
InBlock.gif                                i++; 
InBlock.gif                                Helper.Common.SetAttribute(tc,  "class""yy_sgv_fixRow", AttributeValuePosition.Last, ' '); 
InBlock.gif                        } 
InBlock.gif                         // 固定列 
InBlock.gif                         if    
InBlock.gif                                ( 
InBlock.gif                                        !String.IsNullOrEmpty( this._sgv.FixRowColumn.FixColumns) 
InBlock.gif                                        && 
InBlock.gif                                        Array.Exists( this._sgv.FixRowColumn.FixColumns.Split(','),  delegate( string s) {  return s == gvtc.ColumnIndex.ToString(); }) 
InBlock.gif                                ) 
InBlock.gif                        { 
InBlock.gif                                i++; 
InBlock.gif                                Helper.Common.SetAttribute(tc,  "class""yy_sgv_fixCol", AttributeValuePosition.Last, ' '); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         // 低等级的z-index 
InBlock.gif                         if (i == 1) 
InBlock.gif                        { 
InBlock.gif                                Helper.Common.SetAttribute(tc,  "class""yy_sgv_fixLow", AttributeValuePosition.Last, ' '); 
InBlock.gif                        } 
InBlock.gif                         // 高等级的z-index 
InBlock.gif                         else  if (i == 2) 
InBlock.gif                        { 
InBlock.gif                                Helper.Common.SetAttribute(tc,  "class""yy_sgv_fixHigh", AttributeValuePosition.Last, ' '); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// RenderBegin 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="sender"></param> 
InBlock.gif                 /// <param name="writer"></param> 
InBlock.gif                 void _sgv_RenderBegin( object sender, HtmlTextWriter writer) 
InBlock.gif                { 
InBlock.gif                        writer.AddStyleAttribute(HtmlTextWriterStyle.Overflow,  "auto"); 
InBlock.gif                        writer.AddStyleAttribute(HtmlTextWriterStyle.Position,  "relative"); 
InBlock.gif                        writer.AddStyleAttribute(HtmlTextWriterStyle.Width, String.IsNullOrEmpty( this._sgv.FixRowColumn.TableWidth) ?  "100%" :  this._sgv.FixRowColumn.TableWidth); 
InBlock.gif                        writer.AddStyleAttribute(HtmlTextWriterStyle.Height, String.IsNullOrEmpty( this._sgv.FixRowColumn.TableHeight) ?  "100%" :  this._sgv.FixRowColumn.TableHeight); 
InBlock.gif                        writer.RenderBeginTag(HtmlTextWriterTag.Div); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// RenderEnd 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="sender"></param> 
InBlock.gif                 /// <param name="writer"></param> 
InBlock.gif                 void _sgv_RenderEnd( object sender, HtmlTextWriter writer) 
InBlock.gif                { 
InBlock.gif                        writer.RenderEndTag(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
InBlock.gif /*正式版的实现 结束*/
 
InBlock.gif /*测试版的实现 开始*/
 
介绍
平时使用GridView的时候会有固定表头、指定行或指定列的需求,就像Excel冻结行、列那样。其实我们可以用CSS来搞定。扩展一下GridView,通过设置几个属性来达到这样的功能。


控件开发
1、新建一个继承自GridView的类,另外为了保持滚动条状态,还要继承IPostBackDataHandler接口
/// <summary> 
InBlock.gif         /// 继承自GridView 
InBlock.gif         /// </summary> 
InBlock.gif        [ToolboxData( @"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")] 
InBlock.gif         public  class SmartGridView : GridView, IPostBackDataHandler 
InBlock.gif        { 
InBlock.gif 
InBlock.gif        }
 
2、新建一个FixRowCol类,有七个属性
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ComponentModel; 
InBlock.gif 
InBlock.gif namespace YYControls.SmartGridView 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 固定表头、指定行或指定列的实体类 
InBlock.gif         /// </summary> 
InBlock.gif        [TypeConverter( typeof(ExpandableObjectConverter))] 
InBlock.gif         public  class FixRowCol 
InBlock.gif        { 
InBlock.gif                 private  bool _isFixHeader; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 固定表头否? 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "固定表头否?"), Category( "扩展"), DefaultValue( false), NotifyParentProperty( true)] 
InBlock.gif                 public  virtual  bool IsFixHeader 
InBlock.gif                { 
InBlock.gif                        get {  return _isFixHeader; } 
InBlock.gif                        set { _isFixHeader = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  bool _isFixPager; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 固定分页行否? 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "固定分页行否?"), Category( "扩展"), DefaultValue( false), NotifyParentProperty( true)] 
InBlock.gif                 public  virtual  bool IsFixPager 
InBlock.gif                { 
InBlock.gif                        get {  return _isFixPager; } 
InBlock.gif                        set { _isFixPager = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  string _fixRowIndices; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 需要固定的行的索引(用逗号“,”分隔) 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "需要固定的行的索引(用逗号“,”分隔)"), Category( "扩展"), NotifyParentProperty( true)] 
InBlock.gif                 public  virtual  string FixRowIndices 
InBlock.gif                { 
InBlock.gif                        get {  return _fixRowIndices; } 
InBlock.gif                        set { _fixRowIndices = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  string _fixColumnIndices; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 需要固定的列的索引(用逗号“,”分隔) 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "需要固定的列的索引(用逗号“,”分隔)"), Category( "扩展"), NotifyParentProperty( true)] 
InBlock.gif                 public  virtual  string FixColumnIndices 
InBlock.gif                { 
InBlock.gif                        get {  return _fixColumnIndices; } 
InBlock.gif                        set { _fixColumnIndices = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private System.Web.UI.WebControls.Unit _tableWidth; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 表格的宽度 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "表格的宽度"), Category( "扩展"), NotifyParentProperty( true)] 
InBlock.gif                 public System.Web.UI.WebControls.Unit TableWidth 
InBlock.gif                { 
InBlock.gif                        get {  return _tableWidth; } 
InBlock.gif                        set { _tableWidth = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private System.Web.UI.WebControls.Unit _tableHeight; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 表格的高度 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "表格的高度"), Category( "扩展"), NotifyParentProperty( true)] 
InBlock.gif                 public System.Web.UI.WebControls.Unit TableHeight 
InBlock.gif                { 
InBlock.gif                        get {  return _tableHeight; } 
InBlock.gif                        set { _tableHeight = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  bool _enableScrollState; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 是否保持滚动条的状态 
InBlock.gif                 /// </summary> 
InBlock.gif                [Description( "是否保持滚动条的状态"), Category( "扩展"), DefaultValue( false), NotifyParentProperty( true)] 
InBlock.gif                 public  bool EnableScrollState 
InBlock.gif                { 
InBlock.gif                        get {  return _enableScrollState; } 
InBlock.gif                        set { _enableScrollState = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// ToString(); 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  override  string ToString() 
InBlock.gif                { 
InBlock.gif                         return  "FixRowCol"
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
3、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第2步创建的那个FixRowCol
InBlock.gif private FixRowCol _fixRowCol; 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 固定表头、指定行或指定列 
InBlock.gif                 /// </summary> 
InBlock.gif                [ 
InBlock.gif                Description( "固定表头、指定行或指定列"), 
InBlock.gif                Category( "扩展"), 
InBlock.gif                DefaultValue(""), 
InBlock.gif                DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
InBlock.gif                PersistenceMode(PersistenceMode.InnerProperty) 
InBlock.gif                ] 
InBlock.gif                 public  virtual FixRowCol FixRowCol 
InBlock.gif                { 
InBlock.gif                        get 
InBlock.gif                        { 
InBlock.gif                                 if (_fixRowCol ==  null
InBlock.gif                                { 
InBlock.gif                                        _fixRowCol =  new FixRowCol(); 
InBlock.gif                                } 
InBlock.gif                                 return _fixRowCol; 
InBlock.gif                        } 
InBlock.gif                }
 
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345514,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值