扩展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,如需转载请自行联系原作者


相关文章
PyQt5 技术篇-QTableWidget表格组件指定行的隐藏与显示控制实例演示,设置表格指定列的列宽方法
PyQt5 技术篇-QTableWidget表格组件指定行的隐藏与显示控制实例演示,设置表格指定列的列宽方法
590 0
PyQt5 技术篇-QTableWidget表格组件指定行的隐藏与显示控制实例演示,设置表格指定列的列宽方法
|
7月前
gridControl在同一列的不同行创建不同的控件(已CheckEidt搭配ButtonEdit为例)
gridControl在同一列的不同行创建不同的控件(已CheckEidt搭配ButtonEdit为例)
|
人工智能 C#
c#中在datagridview的表格动态增加一个按钮方法
c#中在datagridview的表格动态增加一个按钮方法,如果想要这一套教程的可以移步去这里 《期末作业C#实现学生宿舍管理系统》,对了最近我们有一个人工智能交流群,如果大家对代码有问题,想交流的可以进群,私聊我就可以了! 效果图片 : 在Load事件中写入代码 那ui有了功能怎么办呢?别急我们在 dataGridView1_CellContentClick事件中添加方法 这样的话 我们就可以点击对应行的修改来获取到id的值这里有一个bug就是第三行没数据需要隐藏,现在还没有解决,欢迎大家指出!.....
546 0
c#中在datagridview的表格动态增加一个按钮方法
PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数
PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数
550 0
PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数