1 #region 版权信息
2 /*---------------------------------------------------------------------*
3 // Copyright (C) 2010 http://www.cnblogs.com/huyong
4 // 版权所有。
5 // 项目 名称:《Winform通用控件库》
6 // 文 件 名: UcPageControl.cs
7 // 类 全 名: DotNet.Controls.UcPageControl
8 // 描 述: 分页控件
9 // 创建 时间: 2010-06-05
10 // 创建人信息: [**** 姓名:胡勇 QQ:80368704 E-Mail:80368704@qq.com *****]
11 *----------------------------------------------------------------------*/
12 #endregion
13
14 using System;
15 using System.Collections.Generic;
16 using System.ComponentModel;
17 using System.Drawing;
18 using System.Data;
19 using System.Data.SqlClient;
20 using System.Windows.Forms;
21 using DotNet.Common;
22 using DotNet.DBUtility;
23
24 namespace DotNet.Controls
25 {
26 #region 委托申明
27 ///<summary>
28 /// 申明委托
29 ///</summary>
30 ///<param name="e"></param>
31 ///<returns></returns>
32 publicdelegateint EventPagingHandler(EventPagingArg e);
33 #endregion
34
35 #region 分页控件
36 ///<summary>
37 /// 分页控件
38 ///
39 /// 修改纪录(此分页控件经过多次修改,已趋于完美,可放心使用。)
40 /// 2010-12-06 胡勇 对上一条、下一条、首条、末条数据导航的隐藏,因为控件本身已做了处理。
41 /// 2010-12-05 胡勇 对分页控件代码做了相应优化
42 /// 2010-06-05 胡勇 创建分页控件
43 ///
44 ///<author>
45 ///<name>胡勇</name>
46 ///<QQ>80368704</QQ>
47 ///<Email>80368704@qq.com</Email>
48 ///</author>
49 ///</summary>
50 [ToolboxItem(true)]
51 [DefaultEvent("EventPaging")]
52 [ToolboxBitmap(typeof(UcPageControl), "DotNet.Controls.Images.UcPageControl.bmp")]
53 [Description("分页控件")]
54 publicpartialclass UcPageControl : UserControl
55 {
56 #region 申明事件
57 ///<summary>
58 /// 单击移动到当前页上一末记录时发生
59 ///</summary>
60 [Category("数据分页"), Description("单击移动到当前页上一末记录时发生。"),Browsable(false)]
61 publicevent EventHandler OnBindingNavigatorMovePreviousItemClick;
62
63 ///<summary>
64 /// 单击移动到当前页第一条记录时发生
65 ///</summary>
66 [Category("数据分页"), Description("单击移动到当前页第一条记录时发生。"), Browsable(false)]
67 publicevent EventHandler OnBindingNavigatorMoveFirstItemClick;
68
69 ///<summary>
70 /// 单击移动到当前页下一条记录时发生
71 ///</summary>
72 [Category("数据分页"), Description("单击移动到当前页下一条记录时发生。"), Browsable(false)]
73 publicevent EventHandler OnBindingNavigatorMoveNextItemClick;
74
75 ///<summary>
76 /// 单击移动到当前页最后一条记录时发生
77 ///</summary>
78 [Category("数据分页"), Description("单击移动到当前页最后一条记录时发生。"), Browsable(false)]
79 publicevent EventHandler OnBindingNavigatorMoveLastItemClick;
80
81 ///<summary>
82 /// 单击各分页按钮(上一页、下一页、第一页、最后一页和转到某页)时发生
83 ///</summary>
84 [Category("数据分页"), Description("分页时发生。")]
85 publicevent EventPagingHandler EventPaging;
86 #endregion
87
88 #region 构造函数
89 public UcPageControl()
90 {
91 InitializeComponent();
92 }
93 #endregion
94
95 #region 属性
96
97 privateint _pageSize =50; //每页显示记录数
98 privateint _nMax =0; //总记录数
99 privateint _pageCount =0; //页数=总记录数/每页显示记录数
100 privateint _pageCurrent =0; //当前页号
101
102 ///<summary>
103 /// 每页显示记录数
104 ///</summary>
105 [Category("数据分页"), Description("每页显示记录数。"), Browsable(false)]
106 publicint PageSize
107 {
108 get
109 {
110 return _pageSize;
111 }
112 set
113 {
114 _pageSize = value;
115 GetPageCount();//页数
116 }
117 }
118
119 ///<summary>
120 /// 记录总数
121 ///</summary>
122 [Category("数据分页"), Description("记录总数。"),Browsable(false)]
123 publicint NMax
124 {
125 get
126 {
127 return _nMax;
128 }
129 set
130 {
131 _nMax = value;
132 GetPageCount();
133 }
134 }
135
136 ///<summary>
137 /// 页数
138 ///</summary>
139 [Category("数据分页"), Description("页数。"), Browsable(false)]
140 publicint PageCount
141 {
142 get
143 {
144 return _pageCount;
145 }
146 set
147 {
148 _pageCount = value;
149 }
150 }
151
152 ///<summary>
153 /// 当前页号
154 ///</summary>
155 [Category("数据分页"), Description("当前页号。"), Browsable(false)]
156 publicint PageCurrent
157 {
158 get
159 {
160 return _pageCurrent;
161 }
162 set
163 {
164 _pageCurrent = value;
165 }
166 }
167 #endregion
168
169 #region 方法
170 [Category("数据分页"), Description("bindingNavigator。"), Browsable(false)]
171 public BindingNavigator ToolBar
172 {
173 get
174 {
175 returnthis.bindingNavigator;
176 }
177 }
178
179 ///<summary>
180 /// 得到总页数
181 ///</summary>
182 privatevoid GetPageCount()
183 {
184 if (this.NMax >0)
185 {
186 this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
187 }
188 else
189 {
190 this.PageCount =0;
191 }
192 }
193
194 ///<summary>
195 /// 绑定分页控件(关键代码)
196 ///</summary>
197 publicvoid Bind()
198 {
199 if (this.EventPaging !=null)
200 {
201 this.NMax =this.EventPaging(new EventPagingArg(this.PageCurrent));
202 }
203
204 if (this.PageCurrent >this.PageCount)
205 {
206 this.PageCurrent =this.PageCount;
207 }
208 if (this.PageCount ==1)
209 {
210 this.PageCurrent =1;
211 }
212 lblPageCount.Text =this.PageCount.ToString();
213 this.lblMaxPage.Text ="共"+this.NMax.ToString()+"条记录";
214 this.txtCurrentPage.Text =this.PageCurrent.ToString();
215
216 if (this.PageCurrent ==1)
217 {
218 this.btnPrev.Enabled =false;
219 this.btnFirst.Enabled =false;
220 }
221 else
222 {
223 btnPrev.Enabled =true;
224 btnFirst.Enabled =true;
225 }
226
227 if (this.PageCurrent ==this.PageCount)
228 {
229 this.btnLast.Enabled =false;
230 this.btnNext.Enabled =false;
231 }
232 else
233 {
234 btnLast.Enabled =true;
235 btnNext.Enabled =true;
236 }
237
238 if (this.NMax ==0)
239 {
240 btnNext.Enabled =false;
241 btnLast.Enabled =false;
242 btnFirst.Enabled =false;
243 btnPrev.Enabled =false;
244 }
245 }
246
247 #endregion
248
249 #region 按钮事件
250 privatevoid btnFirst_Click(object sender, EventArgs e)
251 {
252 PageCurrent =1;
253 this.Bind();
254 }
255
256 privatevoid btnPrev_Click(object sender, EventArgs e)
257 {
258 PageCurrent -=1;
259 if (PageCurrent <=0)
260 {
261 PageCurrent =1;
262 }
263 this.Bind();
264 }
265
266 privatevoid btnNext_Click(object sender, EventArgs e)
267 {
268 this.PageCurrent +=1;
269 if (PageCurrent > PageCount)
270 {
271 PageCurrent = PageCount;
272 }
273 this.Bind();
274 }
275
276 privatevoid btnLast_Click(object sender, EventArgs e)
277 {
278 PageCurrent = PageCount;
279 this.Bind();
280 }
281
282 privatevoid btnGo_Click(object sender, EventArgs e)
283 {
284 if (this.txtCurrentPage.Text !=null&& txtCurrentPage.Text !="")
285 {
286 if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
287 {
288 this.Bind();
289 }
290 else
291 {
292 DialogHelper.ShowErrorMsg("输入数字格式错误!");
293 }
294 }
295 }
296
297 privatevoid txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
298 {
299 if (e.KeyCode == Keys.Enter)
300 {
301 this.Bind();
302 }
303 }
304
305 privatevoid bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
306 {
307 if(OnBindingNavigatorMovePreviousItemClick !=null)
308 {
309 OnBindingNavigatorMovePreviousItemClick(this, null);
310 }
311 }
312
313 privatevoid bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
314 {
315 if (OnBindingNavigatorMoveFirstItemClick !=null)
316 {
317 OnBindingNavigatorMoveFirstItemClick(this, null);
318 }
319 }
320
321 privatevoid bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
322 {
323 if (OnBindingNavigatorMoveNextItemClick !=null)
324 {
325 OnBindingNavigatorMoveNextItemClick(this, null);
326 }
327 }
328
329 privatevoid bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
330 {
331 if (OnBindingNavigatorMoveLastItemClick !=null)
332 {
333 OnBindingNavigatorMoveLastItemClick(this, null);
334 }
335 }
336 #endregion
337 }
338 #endregion
339
340 #region 自定义事件数据基类
341 ///<summary>
342 /// 自定义事件数据基类
343 ///</summary>
344 publicclass EventPagingArg : EventArgs
345 {
346 privateint _intPageIndex;
347 public EventPagingArg(int PageIndex)
348 {
349 _intPageIndex = PageIndex;
350 }
351 }
352 #endregion
353
354 #region 数据源提供(PageData)
355 ///<summary>
356 /// 数据源提供
357 ///</summary>
358 publicclass PageData
359 {
360 DataSet ds =null;
361 privateint _PageSize =50; //分页大小
362 privateint _PageIndex =1; //当前页
363 privateint _PageCount =0; //总页数
364 privateint _TotalCount =0; //总记录数
365 privatestring _QueryFieldName ="*"; //表字段FieldStr
366 privatebool _isQueryTotalCounts =true; //是否查询总的记录条数
367 privatestring _TableName =string.Empty; //表名
368 privatestring _OrderStr =string.Empty; //排序_SortStr
369 privatestring _QueryCondition =string.Empty; //查询的条件 RowFilter
370 privatestring _PrimaryKey =string.Empty; //主键
371
372 ///<summary>
373 /// 是否查询总的记录条数
374 ///</summary>
375 publicbool IsQueryTotalCounts
376 {
377 get { return _isQueryTotalCounts; }
378 set { _isQueryTotalCounts = value; }
379 }
380
381 ///<summary>
382 /// 分页大小(每页显示多少条数据)
383 ///</summary>
384 publicint PageSize
385 {
386 get
387 {
388 return _PageSize;
389
390 }
391 set
392 {
393 _PageSize = value;
394 }
395 }
396
397 ///<summary>
398 /// 当前页
399 ///</summary>
400 publicint PageIndex
401 {
402 get
403 {
404 return _PageIndex;
405 }
406 set
407 {
408 _PageIndex = value;
409 }
410 }
411
412 ///<summary>
413 /// 总页数
414 ///</summary>
415 publicint PageCount
416 {
417 get
418 {
419 return _PageCount;
420 }
421 }
422
423 ///<summary>
424 /// 总记录数
425 ///</summary>
426 publicint TotalCount
427 {
428 get
429 {
430 return _TotalCount;
431 }
432 }
433
434 ///<summary>
435 /// 表名或视图名
436 ///</summary>
437 publicstring TableName
438 {
439 get
440 {
441 return _TableName;
442 }
443 set
444 {
445 _TableName = value;
446 }
447 }
448
449 ///<summary>
450 /// 表字段FieldStr
451 ///</summary>
452 publicstring QueryFieldName
453 {
454 get
455 {
456 return _QueryFieldName;
457 }
458 set
459 {
460 _QueryFieldName = value;
461 }
462 }
463
464 ///<summary>
465 /// 排序字段
466 ///</summary>
467 publicstring OrderStr
468 {
469 get
470 {
471 return _OrderStr;
472 }
473 set
474 {
475 _OrderStr = value;
476 }
477 }
478
479 ///<summary>
480 /// 查询条件
481 ///</summary>
482 publicstring QueryCondition
483 {
484 get
485 {
486 return _QueryCondition;
487 }
488 set
489 {
490 _QueryCondition = value;
491 }
492 }
493
494 ///<summary>
495 /// 主键
496 ///</summary>
497 publicstring PrimaryKey
498 {
499 get
500 {
501 return _PrimaryKey;
502 }
503 set
504 {
505 _PrimaryKey = value;
506 }
507 }
508
509 ///<summary>
510 /// 得到分页数据
511 ///</summary>
512 ///<param name="connectionstring">连接字符串</param>
513 ///<returns>DataSet</returns>
514 public DataSet QueryDataTable(string connectionstring)
515 {
516 SqlParameter[] parameters = {
517 new SqlParameter("@Tables", SqlDbType.VarChar, 255),
518 new SqlParameter("@PrimaryKey" , SqlDbType.VarChar , 255),
519 new SqlParameter("@Sort", SqlDbType.VarChar , 255),
520 new SqlParameter("@CurrentPage", SqlDbType.Int ),
521 new SqlParameter("@PageSize", SqlDbType.Int ),
522 new SqlParameter("@Fields", SqlDbType.VarChar, 255),
523 new SqlParameter("@Filter", SqlDbType.VarChar, 1000),
524 new SqlParameter("@Group" , SqlDbType.VarChar, 1000)
525 };
526 parameters[0].Value = _TableName;
527 parameters[1].Value = _PrimaryKey;
528 parameters[2].Value = _OrderStr;
529 parameters[3].Value = PageIndex;
530 parameters[4].Value = PageSize;
531 parameters[5].Value =_QueryFieldName;
532 parameters[6].Value = _QueryCondition;
533 parameters[7].Value =string.Empty;
534 ds =null;
535 ds =new DataSet();
536 ds = DbHelperSQL.RunProcedure(connectionstring, "uspDividePage", parameters, "tbPageData");
537
538 if (_isQueryTotalCounts)
539 {
540 _TotalCount = GetTotalCount(connectionstring);
541 }
542
543 if (_TotalCount ==0)
544 {
545 _PageIndex =0;
546 _PageCount =0;
547 }
548 else
549 {
550 _PageCount = _TotalCount % _PageSize ==0? _TotalCount / _PageSize : _TotalCount / _PageSize +1;
551
552 if (_PageIndex > _PageCount)
553 {
554 _PageIndex = _PageCount;
555 parameters[4].Value = _PageSize;
556 ds = QueryDataTable(connectionstring);
557 }
558 }
559
560 return ds;
561 }
562
563 ///<summary>
564 /// 得到总的记录数
565 ///</summary>
566 ///<param name="connectionstring">连接字符串</param>
567 ///<returns>总的记录数</returns>
568 publicint GetTotalCount(string connectionstring)
569 {
570 string strSql =" select count(1) from "+_TableName;
571
572 if (_QueryCondition !=string.Empty)
573 {
574 strSql +=" where "+ _QueryCondition;
575 }
576
577 return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString(), connectionstring));
578 }
579 }
580 #endregion
581 }
582