Pager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using ProjectBase.Data.Pager;
  12. namespace ProjectBase.Controls
  13. {
  14. public delegate void PageChangedEventHandler(object sender, EventArgs e);
  15. public delegate void ExportCurrentEventHandler(object sender, EventArgs e);
  16. public delegate void ExportAllEventHandler(object sender, EventArgs e);
  17. /// <summary>
  18. /// 分页工具条用户控件,仅提供分页信息显示及改变页码操作
  19. /// </summary>
  20. public partial class Pager : DevExpress.XtraEditors.XtraUserControl
  21. {
  22. /// <summary>
  23. /// 页面切换的时候触发的事件
  24. /// </summary>
  25. public event PageChangedEventHandler PageChanged;
  26. public event ExportCurrentEventHandler ExportCurrent;
  27. public event ExportAllEventHandler ExportAll;
  28. private int m_PageSize;
  29. private int m_PageCount;
  30. private int m_RecordCount;
  31. private int m_CurrentPageIndex;
  32. private bool m_ShowExportButton = true;//是否显示导出按钮
  33. private PagerInfo pagerInfo = null;
  34. /// <summary>
  35. /// 分页信息
  36. /// </summary>
  37. public PagerInfo PagerInfo
  38. {
  39. get
  40. {
  41. if (pagerInfo == null)
  42. {
  43. pagerInfo = new PagerInfo();
  44. pagerInfo.RecordCount = this.RecordCount;
  45. pagerInfo.CurrenetPageIndex = this.CurrentPageIndex;
  46. pagerInfo.PageSize = this.PageSize;
  47. pagerInfo.OnPageInfoChanged += new PageInfoChanged(pagerInfo_OnPageInfoChanged);
  48. }
  49. else
  50. {
  51. pagerInfo.CurrenetPageIndex = this.CurrentPageIndex;
  52. }
  53. return pagerInfo;
  54. }
  55. }
  56. void pagerInfo_OnPageInfoChanged(PagerInfo info)
  57. {
  58. this.RecordCount = info.RecordCount;
  59. this.CurrentPageIndex = info.CurrenetPageIndex;
  60. this.PageSize = info.PageSize;
  61. this.InitPageInfo();
  62. }
  63. /// <summary>
  64. /// 必需的设计器变量。在Designer中
  65. /// </summary>
  66. //private System.ComponentModel.Container components = null;
  67. /// <summary>
  68. /// 默认构造函数,设置分页初始信息
  69. /// </summary>
  70. public Pager()
  71. {
  72. InitializeComponent();
  73. this.m_PageSize = 50;
  74. this.m_RecordCount = 0;
  75. this.m_CurrentPageIndex = 1; //默认为第一页
  76. this.InitPageInfo();
  77. }
  78. /// <summary>
  79. /// 带参数的构造函数
  80. /// <param name="pageSize">每页记录数</param>
  81. /// <param name="recordCount">总记录数</param>
  82. /// </summary>
  83. public Pager(int recordCount, int pageSize)
  84. {
  85. InitializeComponent();
  86. this.m_PageSize = pageSize;
  87. this.m_RecordCount = recordCount;
  88. this.m_CurrentPageIndex = 1; //默认为第一页
  89. this.InitPageInfo();
  90. }
  91. /// <summary>
  92. /// 清理所有正在使用的资源。在Designer中
  93. /// </summary>
  94. //protected override void Dispose(bool disposing)
  95. //{
  96. // if (disposing)
  97. // {
  98. // if (components != null)
  99. // {
  100. // components.Dispose();
  101. // }
  102. // }
  103. // base.Dispose(disposing);
  104. //}
  105. /// <summary>
  106. /// 页面变化处理
  107. /// </summary>
  108. /// <param name="e"></param>
  109. protected virtual void OnPageChanged(EventArgs e)
  110. {
  111. if (PageChanged != null)
  112. {
  113. PageChanged(this, e);
  114. }
  115. }
  116. /// <summary>
  117. /// 是否显示导出按钮
  118. /// </summary>
  119. [Description("是否显示导出按钮。"), DefaultValue(true), Category("分页")]
  120. public bool ShowExportButton
  121. {
  122. get { return m_ShowExportButton; }
  123. set
  124. {
  125. m_ShowExportButton = value;
  126. this.btn_Export.Visible = value;
  127. this.btn_ExportCurrent.Visible = value;
  128. }
  129. }
  130. /// <summary>
  131. /// 设置或获取一页中显示的记录数目
  132. /// </summary>
  133. [Description("设置或获取一页中显示的记录数目"), DefaultValue(50), Category("分页")]
  134. public int PageSize
  135. {
  136. set
  137. {
  138. this.m_PageSize = value;
  139. }
  140. get
  141. {
  142. return this.m_PageSize;
  143. }
  144. }
  145. /// <summary>
  146. /// 获取记录总页数
  147. /// </summary>
  148. [Description("获取记录总页数"), DefaultValue(0), Category("分页")]
  149. public int PageCount
  150. {
  151. get
  152. {
  153. return this.m_PageCount;
  154. }
  155. }
  156. /// <summary>
  157. /// 设置或获取记录总数
  158. /// </summary>
  159. [Description("设置或获取记录总数"), Category("分页")]
  160. public int RecordCount
  161. {
  162. set
  163. {
  164. this.m_RecordCount = value;
  165. }
  166. get
  167. {
  168. return this.m_RecordCount;
  169. }
  170. }
  171. /// <summary>
  172. /// 当前的页面索引, 开始为1
  173. /// </summary>
  174. [Description("当前的页面索引, 开始为1"), DefaultValue(0), Category("分页")]
  175. [Browsable(false)]
  176. public int CurrentPageIndex
  177. {
  178. set
  179. {
  180. this.m_CurrentPageIndex = value;
  181. }
  182. get
  183. {
  184. return this.m_CurrentPageIndex;
  185. }
  186. }
  187. /// <summary>
  188. /// 初始化分页信息
  189. /// </summary>
  190. /// <param name="info"></param>
  191. public void InitPageInfo(PagerInfo info)
  192. {
  193. this.m_RecordCount = info.RecordCount;
  194. this.m_PageSize = info.PageSize;
  195. this.InitPageInfo();
  196. }
  197. /// <summary>
  198. /// 初始化分页信息
  199. /// <param name="pageSize">每页记录数</param>
  200. /// <param name="recordCount">总记录数</param>
  201. /// </summary>
  202. public void InitPageInfo(int recordCount, int pageSize)
  203. {
  204. this.m_RecordCount = recordCount;
  205. this.m_PageSize = pageSize;
  206. this.InitPageInfo();
  207. }
  208. /// <summary>
  209. /// 初始化分页信息
  210. /// <param name="recordCount">总记录数</param>
  211. /// </summary>
  212. public void InitPageInfo(int recordCount)
  213. {
  214. this.m_RecordCount = recordCount;
  215. this.InitPageInfo();
  216. }
  217. /// <summary>
  218. /// 初始化分页信息
  219. /// </summary>
  220. public void InitPageInfo()
  221. {
  222. if (this.m_PageSize < 1)
  223. this.m_PageSize = 10; //如果每页记录数不正确,即更改为10
  224. if (this.m_RecordCount < 0)
  225. this.m_RecordCount = 0; //如果记录总数不正确,即更改为0
  226. //取得总页数
  227. if (this.m_RecordCount % this.m_PageSize == 0)
  228. {
  229. this.m_PageCount = this.m_RecordCount / this.m_PageSize;
  230. }
  231. else
  232. {
  233. this.m_PageCount = this.m_RecordCount / this.m_PageSize + 1;
  234. }
  235. //设置当前页
  236. if (this.m_CurrentPageIndex > this.m_PageCount)
  237. {
  238. this.m_CurrentPageIndex = this.m_PageCount;
  239. }
  240. if (this.m_CurrentPageIndex < 1)
  241. {
  242. this.m_CurrentPageIndex = 1;
  243. }
  244. //设置按钮的可用性
  245. bool enable = (this.CurrentPageIndex > 1);
  246. this.btn_Previous.Enabled = enable;
  247. enable = (this.CurrentPageIndex < this.PageCount);
  248. this.btn_Next.Enabled = enable;
  249. this.txt_CurrentPage.Text = this.m_CurrentPageIndex.ToString();
  250. this.label_PageInfo.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", this.m_RecordCount, this.m_PageSize, this.m_PageCount);
  251. this.btn_Export.Enabled = this.ShowExportButton;
  252. this.btn_ExportCurrent.Enabled = this.ShowExportButton;
  253. }
  254. /// <summary>
  255. /// 刷新页面数据
  256. /// </summary>
  257. /// <param name="page">页码</param>
  258. public void RefreshData(int page)
  259. {
  260. this.m_CurrentPageIndex = page;
  261. EventArgs e = new EventArgs();
  262. OnPageChanged(e);
  263. }
  264. private void btn_First_Click(object sender, EventArgs e)
  265. {
  266. this.RefreshData(1);
  267. }
  268. private void btn_Previous_Click(object sender, EventArgs e)
  269. {
  270. if (this.m_CurrentPageIndex > 1)
  271. {
  272. this.RefreshData(this.m_CurrentPageIndex - 1);
  273. }
  274. else
  275. {
  276. this.RefreshData(1);
  277. }
  278. }
  279. private void btn_Next_Click(object sender, EventArgs e)
  280. {
  281. if (this.m_CurrentPageIndex < this.m_PageCount)
  282. {
  283. this.RefreshData(this.m_CurrentPageIndex + 1);
  284. }
  285. else if (this.m_PageCount < 1)
  286. {
  287. this.RefreshData(1);
  288. }
  289. else
  290. {
  291. this.RefreshData(this.m_PageCount);
  292. }
  293. }
  294. private void btn_Last_Click(object sender, EventArgs e)
  295. {
  296. if (this.m_PageCount > 0)
  297. {
  298. this.RefreshData(this.m_PageCount);
  299. }
  300. else
  301. {
  302. this.RefreshData(1);
  303. }
  304. }
  305. private void txt_CurrentPage_KeyDown(object sender, KeyEventArgs e)
  306. {
  307. if (e.KeyCode == Keys.Enter)
  308. {
  309. int num;
  310. try
  311. {
  312. num = Convert.ToInt16(this.txt_CurrentPage.Text);
  313. }
  314. catch
  315. {
  316. num = 1;
  317. }
  318. if (num > this.m_PageCount)
  319. num = this.m_PageCount;
  320. if (num < 1)
  321. num = 1;
  322. this.RefreshData(num);
  323. }
  324. }
  325. private void btn_ExportCurrent_Click(object sender, EventArgs e)
  326. {
  327. if (ExportCurrent != null)
  328. {
  329. ExportCurrent(sender, e);
  330. }
  331. }
  332. private void btn_Export_Click(object sender, EventArgs e)
  333. {
  334. if (ExportAll != null)
  335. {
  336. ExportAll(sender, e);
  337. }
  338. }
  339. }
  340. }