PagerEx.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.ComponentLibrary
  10. {
  11. public partial class PagerEx : UserControl
  12. {
  13. /// 声明委托
  14. /// </summary>
  15. /// <param name="e"></param>
  16. public delegate void EventPagingHandler(EventArgs e);
  17. /// <summary>
  18. /// 构造函数
  19. /// </summary>
  20. public PagerEx()
  21. {
  22. InitializeComponent();
  23. }
  24. public event EventPagingHandler EventPaging;
  25. #region 公开属性
  26. private int _pageSize;
  27. /// <summary>
  28. /// 每页显示记录数(默认10)
  29. /// </summary>
  30. public int PageSize
  31. {
  32. get
  33. {
  34. return _pageSize;
  35. }
  36. set
  37. {
  38. if (value > 0)
  39. {
  40. _pageSize = value;
  41. }
  42. else
  43. {
  44. _pageSize = 50;
  45. }
  46. this.comboPageSize.Text = _pageSize.ToString();
  47. }
  48. }
  49. private int _currentPage = 1;
  50. /// <summary>
  51. /// 当前页
  52. /// </summary>
  53. public int CurrentPage
  54. {
  55. get
  56. {
  57. return _currentPage;
  58. }
  59. set
  60. {
  61. if (value > 0)
  62. {
  63. _currentPage = value;
  64. }
  65. else
  66. {
  67. _currentPage = 1;
  68. }
  69. }
  70. }
  71. private int _totalCount = 0;
  72. /// <summary>
  73. /// 总记录数
  74. /// </summary>
  75. public int TotalCount
  76. {
  77. get
  78. {
  79. return _totalCount;
  80. }
  81. set
  82. {
  83. if (value >= 0)
  84. {
  85. _totalCount = value;
  86. }
  87. else
  88. {
  89. _totalCount = 0;
  90. }
  91. this.lblTotalCount.Text = this._totalCount.ToString();
  92. CalculatePageCount();
  93. }
  94. }
  95. private int _pageCount = 0;
  96. /// <summary>
  97. /// 页数
  98. /// </summary>
  99. public int PageCount
  100. {
  101. get
  102. {
  103. return _pageCount;
  104. }
  105. set
  106. {
  107. if (value >= 0)
  108. {
  109. _pageCount = value;
  110. }
  111. else
  112. {
  113. _pageCount = 0;
  114. }
  115. this.lblPageCount.Text = _pageCount + "";
  116. }
  117. }
  118. #endregion
  119. #region 算法与绑定
  120. /// <summary>
  121. /// 计算页数
  122. /// </summary>
  123. private void CalculatePageCount()
  124. {
  125. //总条数大于0
  126. if (this.TotalCount > 0)
  127. {
  128. this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.TotalCount) / Convert.ToDouble(this.PageSize)));
  129. if (this.CurrentPage > this.PageCount)
  130. {
  131. this.CurrentPage = this.PageCount;
  132. }
  133. this.txtBoxCurPage.Text = this.CurrentPage + "";
  134. this.lblTotalCount.Text = this.TotalCount + "";
  135. this.lblPageCount.Text = this.PageCount + "";
  136. if (this.CurrentPage == 1)
  137. {
  138. this.btnFirst.Enabled = false;
  139. this.btnPrev.Enabled = false;
  140. this.lbtnGO.Enabled = true;
  141. }
  142. else
  143. {
  144. this.btnFirst.Enabled = true;
  145. this.btnPrev.Enabled = true;
  146. }
  147. if (this.CurrentPage == this.PageCount)
  148. {
  149. this.btnNext.Enabled = false;
  150. this.btnLast.Enabled = false;
  151. this.lbtnGO.Enabled = true;
  152. }
  153. else
  154. {
  155. this.btnLast.Enabled = true;
  156. this.btnNext.Enabled = true;
  157. }
  158. //总数等于0
  159. if (this.TotalCount == 0)
  160. {
  161. this.btnFirst.Enabled = false;
  162. this.btnPrev.Enabled = false;
  163. this.btnNext.Enabled = false;
  164. this.btnLast.Enabled = false;
  165. this.lbtnGO.Enabled = false;
  166. this.txtBoxCurPage.Text = "";
  167. }
  168. if (this.CurrentPage < this.PageCount)
  169. {
  170. this.btnNext.Enabled = true;
  171. }
  172. else { this.btnNext.Enabled = false; }
  173. }//总条数为空
  174. else
  175. {
  176. this.PageCount = 0;
  177. this.btnFirst.Enabled = false;
  178. this.btnPrev.Enabled = false;
  179. this.btnNext.Enabled = false;
  180. this.btnLast.Enabled = false;
  181. this.lbtnGO.Enabled = false;
  182. this.txtBoxCurPage.Text = "";
  183. }
  184. }
  185. /// <summary>
  186. /// 数据绑定
  187. /// </summary>
  188. public void Bind()
  189. {
  190. if (this.EventPaging != null)
  191. {
  192. this.EventPaging(new EventArgs());
  193. }
  194. if (this.CurrentPage > this.PageCount)
  195. {
  196. this.CurrentPage = this.PageCount;
  197. }
  198. this.txtBoxCurPage.Text = this.CurrentPage + "";
  199. this.lblTotalCount.Text = this.TotalCount + "";
  200. this.lblPageCount.Text = this.PageCount + "";
  201. if (this.CurrentPage == 1)
  202. {
  203. this.btnFirst.Enabled = false;
  204. this.btnPrev.Enabled = false;
  205. }
  206. else
  207. {
  208. this.btnFirst.Enabled = true;
  209. this.btnPrev.Enabled = true;
  210. }
  211. if (this.CurrentPage == this.PageCount)
  212. {
  213. this.btnNext.Enabled = false;
  214. this.btnLast.Enabled = false;
  215. this.lbtnGO.Enabled = true;
  216. }
  217. else
  218. {
  219. this.btnLast.Enabled = true;
  220. this.btnNext.Enabled = true;
  221. }
  222. //总数等于0
  223. if (this.TotalCount == 0)
  224. {
  225. this.btnFirst.Enabled = false;
  226. this.btnPrev.Enabled = false;
  227. this.btnNext.Enabled = false;
  228. this.btnLast.Enabled = false;
  229. this.lbtnGO.Enabled = false;
  230. this.txtBoxCurPage.Text = "";
  231. }
  232. if (this.CurrentPage < this.PageCount)
  233. {
  234. this.btnNext.Enabled = true;
  235. }
  236. else { this.btnNext.Enabled = false; }
  237. }
  238. #endregion
  239. #region 按钮事件
  240. private void btnFirst_Click(object sender, EventArgs e)
  241. {
  242. this.CurrentPage = 1;
  243. this.Bind();
  244. }
  245. private void btnPrev_Click(object sender, EventArgs e)
  246. {
  247. this.CurrentPage -= 1;
  248. this.Bind();
  249. }
  250. private void btnNext_Click(object sender, EventArgs e)
  251. {
  252. this.CurrentPage += 1;
  253. this.Bind();
  254. }
  255. private void btnLast_Click(object sender, EventArgs e)
  256. {
  257. this.CurrentPage = this.PageCount;
  258. this.Bind();
  259. }
  260. private void lbtnGO_Click(object sender, EventArgs e)
  261. {
  262. try
  263. {
  264. Convert.ToInt32(txtBoxCurPage.Text);
  265. if (Convert.ToInt32(txtBoxCurPage.Text) > PageCount || Convert.ToInt32(txtBoxCurPage.Text) == PageCount)
  266. {
  267. this.txtBoxCurPage.Text = this.PageCount.ToString();
  268. this.CurrentPage = PageCount;
  269. this.Bind();
  270. }
  271. else
  272. {
  273. this.CurrentPage = Convert.ToInt32(txtBoxCurPage.Text);
  274. this.Bind();
  275. }
  276. }
  277. catch
  278. {
  279. MessageBoxCustom.Show("请输入数字");
  280. }
  281. }
  282. private void txtBoxCurPage_KeyPress(object sender, KeyPressEventArgs e)
  283. {
  284. if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
  285. {
  286. MessageBoxCustom.Show("请输入数字");
  287. e.Handled = true;
  288. }
  289. }
  290. #endregion
  291. }
  292. }