webabcd - 专注于asp.net

ASP.NET
从现在开始 一切都不晚
posts - 151, comments - 4188, trackbacks - 344, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理
GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[源码下载]


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


作者:webabcd


/*正式版的实现 开始*/

介绍
扩展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#
using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI.WebControls;
using System.Web.UI;

namespace YYControls.SmartGridViewFunction
{
    
/// <summary>
    
/// 扩展功能:固定指定行、指定列
    
/// </summary>

    public class FixRowColumnFunction : ExtendFunction
    
{
        
/// <summary>
        
/// 构造函数
        
/// </summary>

        public FixRowColumnFunction()
            : 
base()
        
{

        }


        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="sgv">SmartGridView对象</param>

        public FixRowColumnFunction(SmartGridView sgv)
            : 
base(sgv)
        
{

        }


        
/// <summary>
        
/// 扩展功能的实现
        
/// </summary>

        protected override void Execute()
        
{
            
this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
            
this._sgv.RenderBegin += new SmartGridView.RenderBeginHandler(_sgv_RenderBegin);
            
this._sgv.RenderEnd += new SmartGridView.RenderEndHandler(_sgv_RenderEnd);
        }


        
/// <summary>
        
/// SmartGridView的RowDataBoundCell事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="gvtc"></param>

        void _sgv_RowDataBoundCell(object sender, GridViewTableCell gvtc)
        
{
            TableCell tc 
= gvtc.TableCell;
            GridViewRow gvr 
= (GridViewRow)tc.Parent;

            
int i = 0// 0-既不固定行也不固定列;1-固定行或固定列;2-既固定行也固定列
            
// 固定行
            if 
            (
                (
                    
!String.IsNullOrEmpty(this._sgv.FixRowColumn.FixRows) 
                    
&& 
                    Array.Exists(
this._sgv.FixRowColumn.FixRows.Split(','), delegate(string s) return s == gvr.RowIndex.ToString(); })
                )
                
|| 
                (
                    
!String.IsNullOrEmpty(this._sgv.FixRowColumn.FixRowType) 
                    
&& 
                    Array.Exists(
this._sgv.FixRowColumn.FixRowType.Split(','), delegate(string s) return s == gvr.RowType.ToString(); })
                )
                
|| 
                (
                    
!String.IsNullOrEmpty(this._sgv.FixRowColumn.FixRowState) 
                    
&& 
                    Array.Exists(
this._sgv.FixRowColumn.FixRowState.Split(','), delegate(string s) return s == gvr.RowState.ToString(); })
                )
            )
            
{
                i
++;
                Helper.Common.SetAttribute(tc, 
"class""yy_sgv_fixRow", AttributeValuePosition.Last, ' ');
            }

            
// 固定列
            if 
                (
                    
!String.IsNullOrEmpty(this._sgv.FixRowColumn.FixColumns)
                    
&&
                    Array.Exists(
this._sgv.FixRowColumn.FixColumns.Split(','), delegate(string s) return s == gvtc.ColumnIndex.ToString(); })
                )
            
{
                i
++;
                Helper.Common.SetAttribute(tc, 
"class""yy_sgv_fixCol", AttributeValuePosition.Last, ' ');
            }


            
// 低等级的z-index
            if (i == 1)
            
{
                Helper.Common.SetAttribute(tc, 
"class""yy_sgv_fixLow", AttributeValuePosition.Last, ' ');
            }

            
// 高等级的z-index
            else if (i == 2)
            
{
                Helper.Common.SetAttribute(tc, 
"class""yy_sgv_fixHigh", AttributeValuePosition.Last, ' ');
            }

        }


        
/// <summary>
        
/// RenderBegin
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="writer"></param>

        void _sgv_RenderBegin(object sender, HtmlTextWriter writer)
        
{
            writer.AddStyleAttribute(HtmlTextWriterStyle.Overflow, 
"auto");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Position, 
"relative");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, String.IsNullOrEmpty(
this._sgv.FixRowColumn.TableWidth) ? "100%" : this._sgv.FixRowColumn.TableWidth);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, String.IsNullOrEmpty(
this._sgv.FixRowColumn.TableHeight) ? "100%" : this._sgv.FixRowColumn.TableHeight);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }


        
/// <summary>
        
/// RenderEnd
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="writer"></param>

        void _sgv_RenderEnd(object sender, HtmlTextWriter writer)
        
{
            writer.RenderEndTag();
        }

    }

}

/*正式版的实现 结束*/


/*测试版的实现 开始*/

介绍
平时使用GridView的时候会有固定表头、指定行或指定列的需求,就像Excel冻结行、列那样。其实我们可以用CSS来搞定。扩展一下GridView,通过设置几个属性来达到这样的功能。


控件开发
1、新建一个继承自GridView的类,另外为了保持滚动条状态,还要继承IPostBackDataHandler接口
    /// <summary>
    
/// 继承自GridView
    
/// </summary>

    [ToolboxData(@"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")]
    
public class SmartGridView : GridView, IPostBackDataHandler
    
{

    }


2、新建一个FixRowCol类,有七个属性
using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;

namespace YYControls.SmartGridView
{
    
/// <summary>
    
/// 固定表头、指定行或指定列的实体类
    
/// </summary>

    [TypeConverter(typeof(ExpandableObjectConverter))]
    
public class FixRowCol
    
{
        
private bool _isFixHeader;
        
/// <summary>
        
/// 固定表头否?
        
/// </summary>

        [Description("固定表头否?"), Category("扩展"), DefaultValue(false), NotifyParentProperty(true)]
        
public virtual bool IsFixHeader
        
{
            
get return _isFixHeader; }
            
set { _isFixHeader = value; }
        }


        
private bool _isFixPager;
        
/// <summary>
        
/// 固定分页行否?
        
/// </summary>

        [Description("固定分页行否?"), Category("扩展"), DefaultValue(false), NotifyParentProperty(true)]
        
public virtual bool IsFixPager
        
{
            
get return _isFixPager; }
            
set { _isFixPager = value; }
        }


        
private string _fixRowIndices;
        
/// <summary>
        
/// 需要固定的行的索引(用逗号“,”分隔)
        
/// </summary>

        [Description("需要固定的行的索引(用逗号“,”分隔)"), Category("扩展"), NotifyParentProperty(true)]
        
public virtual string FixRowIndices
        
{
            
get return _fixRowIndices; }
            
set { _fixRowIndices = value; }
        }


        
private string _fixColumnIndices;
        
/// <summary>
        
/// 需要固定的列的索引(用逗号“,”分隔)
        
/// </summary>

        [Description("需要固定的列的索引(用逗号“,”分隔)"), Category("扩展"), NotifyParentProperty(true)]
        
public virtual string FixColumnIndices
        
{
            
get return _fixColumnIndices; }
            
set { _fixColumnIndices = value; }
        }


        
private System.Web.UI.WebControls.Unit _tableWidth;
        
/// <summary>
        
/// 表格的宽度
        
/// </summary>

        [Description("表格的宽度"), Category("扩展"), NotifyParentProperty(true)]
        
public System.Web.UI.WebControls.Unit TableWidth
        
{
            
get {&nb