重写GridView,支持CheckBox选择,List排序等等...

重新封装了一个 GridView,支持如下功能:

1. CheckBox选择记录,指定CheckBox的位置
2. 支持List,DataSet,Datatable 排序
3. 排序时在Header部分出现图标
4. 封装了PageIndexChanged 和DataBind,不用每页都写。

1 using System;
2  using System.Collections.Generic;
3  using System.ComponentModel;
4 using System.Data;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Reflection;
9
10 namespace xxWare.xxControls
11 {
12 [ToolboxData("<{0}:xxGridView runat=server></{0}:xxGridView>")]
13 public class xxGridView : System.Web.UI.WebControls.GridView
14 {
15 #region Properties
16 // sort image url : full url = Sort_Image_Url + Asc_Image_Name
17 [Browsable(true) , Category("Sort Image"), DefaultValue("images/arrow_up.gif") , Description("Asc Image")]
18 public string Asc_Image { get; set; }
19 [Browsable(true), Category("Sort Image"), DefaultValue("images/arrow_down.gif"), Description("Desc Image")]
20 public string Desc_Image { get; set; }
21 [Browsable(true), Category("Sort Image"), DefaultValue("images/arrow_updn.gif"), Description("Sortable Image")]
22 public string Sort_Image { get; set; }
23
24 //Property : SortExpression
25 [Browsable(false)]
26 public string CurrentSortExpression
27 {
28 get { return Convert.ToString(ViewState["CurrentSortExpression"]); }
29 set { ViewState["CurrentSortExpression"] = value.Trim(); }
30 }
31 [Browsable(false)]
32 public string CurrentSortDirection
33 {
34 get { return Convert.ToString(ViewState["CurrentSortDirection"]); }
35 set
36 {
37 if (string.IsNullOrEmpty(value))
38 {
39 ViewState["CurrentSortDirection"] = "desc";
40 }
41 else
42 {
43 if (value.ToLower() != "asc" && value.ToLower() != "desc")
44 {
45 ViewState["CurrentSortDirection"] = "desc";
46 }
47 else
48 {
49 ViewState["CurrentSortDirection"] = value.ToString().ToLower();
50 }
51 }
52 }
53 }
54
55 //Property : Multi Show CheckBox
56 [Category("MultiSelect") , DefaultValue("True")]
57 public bool AllowMultiSelect
58 {
59 get { return bool.Parse(ViewState["AllowMultiSelect"].ToString()); }
60 set { ViewState["AllowMultiSelect"] = value.ToString(); }
61 }
62 [Category("MultiSelect"), DefaultValue(0)]
63 public int AllowMultiSelectColumn
64 {
65 get { return Int32.Parse(ViewState["AllowMultiSelectColumn"].ToString()); }
66 set { ViewState["AllowMultiSelectColumn"] = value.ToString(); }
67 }
68 /*
69 public string RowSelectFilter
70 {
71 get { return (string)ViewState["RowSelectFilter"]; }
72 set { ViewState["RowSelectFilter"] = value.ToString(); }
73 }
74 * */
75 //Property : Get Selected Items
76 [Browsable(false)]
77 public List<GridViewRow> MultiSelectedItem
78 {
79 get
80 {
81 if (!AllowMultiSelect) return null;
82
83 List<GridViewRow> selectedRows = new List<GridViewRow>();
84 foreach (GridViewRow row in this.Rows)
85 {
86 CheckBox cb = (CheckBox)row.Cells[AllowMultiSelectColumn].Controls[0];
87 if (cb.Checked)
88 {
89 selectedRows.Add(row);
90 }
91 }
92
93 return selectedRows;
94 }
95 }
96
97 //Define DataSource
98 private object sourcedata;
99 public object DataSetSource
100 {
101 get
102 {
103 if (sourcedata != null)
104 return sourcedata;
105 else
106 return null;
107 }
108 set
109 {
110 sourcedata = value as object;
111 }
112 }
113
114 #endregion
115
116 private ClientScriptManager csManager;
117
118 public xxGridView() : base()
119 {
120 AllowPaging = false;
121 AllowSorting = true;
122 //GridLines = GridLines.Horizontal;
123 //BorderWidth = (Unit)0;
124
125 // Sort Images Default
126 Asc_Image = @"images/arrow_up.gif";
127 Desc_Image = @"images/arrow_down.gif";
128 Sort_Image = @"images/arrow_updn.gif";
129
130 //set event handlers
131 Init += new EventHandler(On_Init);
132 Sorting += new GridViewSortEventHandler(On_Sorting);
133 RowCreated += new GridViewRowEventHandler(On_RowCreated);
134 }
135
136 #region Event Handlers
137 public event EventHandler GridBindEvent;
138 public virtual void OnGridBind<T>()
139 {
140 if (sourcedata!=null)
141 {
142 if (CurrentSortExpression==string.Empty)
143 {
144 this.DataSource = sourcedata;
145 this.DataBind();
146 return;
147 }
148
149 //Datasource Type
150 if (sourcedata is DataTable)
151 {
152 DataView dv = (sourcedata as DataTable).DefaultView;
153 if (!string.IsNullOrEmpty(CurrentSortExpression))
154 dv.Sort = CurrentSortExpression + " " + CurrentSortDirection;
155
156 this.DataSource = dv;
157 this.DataBind();
158 }
159 else if (sourcedata is DataSet)
160 {
161 DataView dv = (sourcedata as DataSet).Tables[0].DefaultView;
162 if (!string.IsNullOrEmpty(CurrentSortExpression))
163 dv.Sort = CurrentSortExpression + " " + CurrentSortDirection;
164
165 this.DataSource = dv;
166 this.DataBind();
167 }
168 else if (sourcedata is List<T>)
169 {
170 if (!string.IsNullOrEmpty(CurrentSortExpression))
171 {
172 Reverser<T> reverser = new Reverser<T>(typeof(T), CurrentSortExpression, CurrentSortDirection);
173 (sourcedata as List<T>).Sort(reverser);
174 }
175
176 this.DataSource = sourcedata;
177 this.DataBind();
178 }
179 }
180
181 }
182
183 public void On_Init(object sender, EventArgs e)
184 {
185 // processing multi-select
186 if (ViewState["AllowMultiSelect"]==null || ViewState["AllowMultiSelect"].ToString().Trim()=="")
187 ViewState["AllowMultiSelect"] = "True";
188 if (ViewState["AllowMultiSelectColumn"] == null || ViewState["AllowMultiSelectColumn"].ToString().Trim() == "")
189 ViewState["AllowMultiSelectColumn"] = "0";
190
191 csManager = this.Page.ClientScript;
192 if (AllowMultiSelect)
193 {
194 AddSelectColumn();
195 RegisterJS();
196 }
197
198 // processing sorting...
199 if (CurrentSortDirection == null || CurrentSortDirection.Trim() == "")
200 CurrentSortDirection = "desc";
201 if (CurrentSortExpression == null) CurrentSortDirection = "";
202 }
203
204 public void On_Sorting(object sender , GridViewSortEventArgs e)
205 {
206 CurrentSortExpression = e.SortExpression;
207 if (CurrentSortDirection == "desc")
208 CurrentSortDirection = "asc";
209 else
210 CurrentSortDirection = "desc";
211
212 GridBindEvent(this , EventArgs.Empty);
213 }
214
215 public void On_RowCreated(object sender , GridViewRowEventArgs e)
216 {
217 string currentSortImage = "";
218 if (e.Row.RowType==DataControlRowType.Header)
219 {
220 foreach (DataControlField field in this.Columns)
221 {
222 if (!String.IsNullOrEmpty(field.SortExpression))
223 {
224 if (IsSortedByThisField(field.SortExpression))
225 {
226 currentSortImage = (CurrentSortDirection == "asc") ? Asc_Image : Desc_Image;
227 }
228 else
229 {
230 currentSortImage = Sort_Image;
231 }
232 AddSortImage(e.Row, this.Columns.IndexOf(field), currentSortImage);
233 }
234 }
235 }
236 }
237
238 #endregion
239
240 #region Override Methods
241 protected override void OnPageIndexChanging(GridViewPageEventArgs e)
242 {
243 //base.OnPageIndexChanging(e);
244 this.PageIndex = e.NewPageIndex;
245 GridBindEvent(this, EventArgs.Empty);
246 }
247 #endregion
248
249 #region private helper function
250 // For Sort
251 private void AddSortImage(GridViewRow _row, int _colIndex ,string _currImage)
252 {
253 if (-1 == _colIndex) return;
254
255 Image sortImage = new Image();
256 sortImage.ImageUrl = _currImage;
257 _row.Cells[_colIndex].Controls.AddAt(1, sortImage);
258 }
259
260 private bool IsSortedByThisField(String strSortExpression)
261 {
262 return CurrentSortExpression.ToLower() == strSortExpression.Trim().ToLower();
263 }
264
265 // for Multi Select
266 private void AddSelectColumn()
267 {
268 TemplateField tc = new TemplateField();
269 tc.ItemTemplate = new FarGridColumnTemplate();
270 tc.HeaderTemplate = new FarGridHeaderTemplate();
271
272 this.Columns.Insert(AllowMultiSelectColumn, tc);
273 }
274 private void RegisterJS()
275 {
276 /*
277 System.Resources.ResourceManager rm = new System.Resources.ResourceManager("xxGridView", Assembly.GetExecutingAssembly());
278 string _script = rm.GetString("js_selectall");
279
280 if (!csManager.IsClientScriptBlockRegistered(this.GetType() , "js_selectall"))
281 {
282 csManager.RegisterClientScriptBlock(this.GetType(), "js_selectall", _script);
283 }
284 */
285
286 csManager.RegisterClientScriptResource(this.GetType(), "xxWare.xxControls.xxGridViewJS.js");
287 }
288
289 #endregion
290
291 }
292 }

还有一些如: MultiSelectFilter 等有时间再完善

posted @ 2011-05-01 04:04  jdxx  阅读(2665)  评论(12编辑  收藏  举报