(一). 概述
1. 做了个GridViewExtend自定义控件, 通过注册扩展自定义表头事件, 可以任意设置Header的格式.
2. 易重用代码, 只要将ExtendGridView.dll 文件添加到项目引用即可.
实现原理:增加一个自定义事件,并将GridView头(HeaderRow)引用作为事件参数传递,使用时通
过注册此事件来自定义头的布局,可以任意定制HeaderRow行数.
(二). 运行截图
(三). 使用步骤
1. 将ExtendGridView.dll 添加到自己的Web站点中, 并在设计器中添加一个GridViewExpendHeader控件.
2. 注册事件, 如图
3. 在后台代码事件中编写重构GridView's HeaderRow的事件
(四). 完整代码
1. 主控件类GridViewExpend.cs代码
1
///
<summary>
2 /// Author: ChengKing(ZhengJian)
3 /// </summary>
4 [ToolboxData( " <{0}:GridViewExpendHeader runat=server></{0}:GridViewExpendHeader> " )]
5 public class GridViewExpendHeader : GridView
6 {
7 [Description( " 当需要重新构造Header时触发此事件 " )]
8 public event System.EventHandler RebuildHeader;
9
10 protected override void Render(HtmlTextWriter writer)
11 {
12 OnRebuildHeader();
13
14 base .Render(writer);
15 }
16 protected override void CreateChildControls()
17 {
18 base .CreateChildControls();
19 }
20
21 protected virtual void OnRebuildHeader()
22 {
23 // 重新构造Head
24 if (RebuildHeader != null )
25 {
26 RebuildHeaderEventArgs args = new RebuildHeaderEventArgs();
27 args.GridViewObject = this ;
28 args.HeaderRowObject = this .HeaderRow;
29
30 RebuildHeader(
2 /// Author: ChengKing(ZhengJian)
3 /// </summary>
4 [ToolboxData( " <{0}:GridViewExpendHeader runat=server></{0}:GridViewExpendHeader> " )]
5 public class GridViewExpendHeader : GridView
6 {
7 [Description( " 当需要重新构造Header时触发此事件 " )]
8 public event System.EventHandler RebuildHeader;
9
10 protected override void Render(HtmlTextWriter writer)
11 {
12 OnRebuildHeader();
13
14 base .Render(writer);
15 }
16 protected override void CreateChildControls()
17 {
18 base .CreateChildControls();
19 }
20
21 protected virtual void OnRebuildHeader()
22 {
23 // 重新构造Head
24 if (RebuildHeader != null )
25 {
26 RebuildHeaderEventArgs args = new RebuildHeaderEventArgs();
27 args.GridViewObject = this ;
28 args.HeaderRowObject = this .HeaderRow;
29
30 RebuildHeader(