当前位置:编程学习 > C#/ASP.NET >>

GridView,DataTable和List排序,SQL分页 支持CheckBox选择

重新封装了一个 gridview,支持如下功能:
1. checkbox选择记录,指定checkbox的位置
2. 支持list,dataset,datatable 排序
3. 排序时在header部分出现图标
4. 封装了pageindexchanged 和databind,不用每页都写。
5. 支持sql分页和aps教程netpager等分页控件。
注: 没有加入很多的功能,因为本身需要的就是一个轻量级的gridview,产生近可能少的代码。
另:选择高亮功能是用jquery实现的,因此使用时需要jquery的运行库。
代码1 : 辅助对象,实现sort排序。(其实这部分功能可以用linq来做,会简单很多,当这个类已经用了很久了,懒得改了)
1 using system;
2  using system.collections;
3  using system.collections.generic;
4 using system.reflection;
5
6 namespace xxware.xxcontrols
7 {
8 public class reverser<t> : icomparer<t>
9 {
10 private type _type = null;
11 private reverserinfo info;
12
13 /// <summary>
14 /// 构造函数
15 /// </summary>
16 /// <param name="type">进行比较的类类型</param>
17 /// <param name="name">进行比较对象的属性名称</param>
18 /// <param name="direction">比较方向(升序/降序)</param>
19 public reverser(type _type, string _name, string _direction)
20 {
21 this._type = _type;
22 this.info.name = _name;
23 this.info.direction = _direction.tolower();
24 }
25
26 /// <summary>
27 /// 构造函数
28 /// </summary>
29 /// <param name="classname">进行比较的类名称</param>
30 /// <param name="name">进行比较对象的属性名称</param>
31 /// <param name="direction">比较方向(升序/降序)</param>
32 public reverser(string _classname, string _name, string _direction)
33 {
34 try
35 {
36 this._type = type.gettype(_classname, true);
37 this.info.name = _name;
38 this.info.direction = _direction.tolower();
39 }
40 catch (exception e)
41 {
42 throw new exception(e.message);
43 }
44
45 }
46
47 /// <summary>
48 /// 构造函数
49 /// </summary>
50 /// <param name="t">进行比较的类型的实例</param>
51 /// <param name="name">进行比较对象的属性名称</param>
52 /// <param name="direction">比较方向(升序/降序)</param>
53 public reverser(t _t, string _name, string _direction)
54 {
55 this._type = _t.gettype();
56 this.info.name = _name;
57 this.info.direction = _direction;
58 }
59
60 int icomparer<t>.compare(t t1, t t2)
61 {
62 object x = this._type.invokemember(this.info.name, bindingflags.public | bindingflags.instance | bindingflags.getproperty, null, t1, null);
63 object y = this._type.invokemember(this.info.name, bindingflags.public | bindingflags.instance | bindingflags.getproperty, null, t2, null);
64
65 if (this.info.direction != "asc")
66 swap(ref x, ref y);
67 return (new caseinsensitivecomparer()).compare(x, y);
68 }
69
70 void swap(ref object x, ref object y)
71 {
72 object tmp = x;
73 x = y;
74 y = tmp;
75 }
76 }
77 public struct reverserinfo
78 {
79 public enum target
80 {
81 customer = 0,
82 from,
83 field,
84 server
85 }
86
87 public string name;
88 public string direction; // asc , desc ;
89 public target target;
90 }
91 }代码2: template类,用于生成checkbox列
1 using system;
2 using system.web;
3 using system.web.ui;
4 using system.web.ui.webcontrols;
5
6 namespace xxware.xxcontrols
7 {
8 #region selector template
9 public class xxgridcolumntemplate : itemplate
10 {
11 public void instantiatein(control container)
12 {
13 checkbox cb = new checkbox();
14 cb.id = "fargv_columnselector";
15 cb.clientidmode = clientidmode.autoid;
16 cb.css教程class = "far_rowsselector";
17 container.controls.addat(0, cb);
18 }
19 }
20
21 public class xxgridheadertemplate : itemplate
22 {
23 public void instantiatein(control container)
24 {
25 system.web.ui.htmlcontrols.htmlinputcheckbox selectall = new system.web.ui.htmlcontrols.htmlinputcheckbox();
26 selectall.id = "fargv_columnselectorall";
27 selectall.clientidmode = clientidmode.static;
28 selectall.attributes["onclick"] = "fargridview_columnselectorall();";
29 container.controls.add(selectall);
30 }
31 }
32
33 #endregion
34
35 }
补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,