CtrlPage.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraPrinting.Native;
  3. using DW5S.DTO;
  4. using DW5S.Entity;
  5. using DW5S.Repostory;
  6. using ExtensionsDev;
  7. using Microsoft.EntityFrameworkCore;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace DW5S.App.UserControl
  16. {
  17. public partial class CtrlPage : DevExpress.XtraEditors.XtraUserControl
  18. {
  19. private event Func<PageQueryDto, Task<DTO.PageData>> OnQueryAsync;
  20. public void SetOnQuery(Func<PageQueryDto, Task<DTO.PageData>> onQueryAsync)
  21. {
  22. this.OnQueryAsync += onQueryAsync;
  23. RefreshData();
  24. }
  25. private bool comPact = false;
  26. /// <summary>
  27. /// 紧凑模式(默认false)
  28. /// </summary>
  29. [ToolboxItem(false)]
  30. [DefaultValue(false)]
  31. [Category("Style"), Description("紧凑模式")]
  32. public bool Compact
  33. {
  34. get
  35. {
  36. return comPact;
  37. }
  38. set
  39. {
  40. comPact = value;
  41. if (comPact)
  42. {
  43. foreach (Control item in this.stackPanel1.Controls)
  44. {
  45. item.Margin = new Padding(2);
  46. if (item.GetType() == typeof(Button))
  47. item.Width = 14;
  48. }
  49. txtCountPerPage.Width = 60;
  50. txtGoPage.Width = 60;
  51. }
  52. else
  53. {
  54. foreach (Control item in this.stackPanel1.Controls)
  55. {
  56. item.Margin = new Padding(2);
  57. if (item.GetType() == typeof(Button))
  58. item.Width = 20;
  59. }
  60. btnFirst.Margin = new System.Windows.Forms.Padding(20, 3, 3, 3);
  61. txtPageInfo.Margin = new System.Windows.Forms.Padding(10, 3, 10, 3);
  62. txtGoPage.Margin = new System.Windows.Forms.Padding(20, 3, 3, 3);
  63. this.txtCountPerPage.Width = 76;
  64. txtGoPage.Width = 76;
  65. }
  66. }
  67. }
  68. public bool showGoto = true;
  69. /// <summary>
  70. /// 是否显示跳转(模式true)
  71. /// </summary>
  72. [ToolboxItem(true)]
  73. [Category("Style"), Description("是否显示跳转")]
  74. [DefaultValue(true)]
  75. public bool ShowGoto
  76. {
  77. get
  78. {
  79. return showGoto;
  80. }
  81. set
  82. {
  83. showGoto = value;
  84. this.txtGoPage.Visible = value;
  85. this.btnGo.Visible = value;
  86. }
  87. }
  88. private bool alignRight = false;
  89. /// <summary>
  90. /// 是否右对齐(默认false)
  91. /// </summary>
  92. [ToolboxItem(false)]
  93. [DefaultValue(false)]
  94. [Category("Style"), Description("右对齐")]
  95. public bool AlignRight
  96. {
  97. get
  98. {
  99. return alignRight;
  100. }
  101. set
  102. {
  103. alignRight = value;
  104. if (alignRight)
  105. {
  106. labelControl1.Text = " ";
  107. stackPanel1.SetStretched(labelControl1, true);
  108. }
  109. else
  110. {
  111. labelControl1.Text = "";
  112. stackPanel1.SetStretched(labelControl1, false);
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 当前页数
  118. /// </summary>
  119. private int CurrentPage { get; set; } = 1;
  120. public CtrlPage()
  121. {
  122. InitializeComponent();
  123. this.MinimumSize = new System.Drawing.Size(100, 26);
  124. }
  125. /// <summary>
  126. /// 刷新数据(当前页会变为1)
  127. /// </summary>
  128. public void RefreshData()
  129. {
  130. btn_Click(btnFirst, null);
  131. }
  132. private void txtCountPerPage_EditValueChanged(object sender, EventArgs e)
  133. {
  134. btn_Click(btnFirst, null);
  135. }
  136. private async void btn_Click(object sender, EventArgs e)
  137. {
  138. try
  139. {
  140. if (OnQueryAsync == null) return;
  141. if (sender == btnFirst)
  142. {
  143. this.CurrentPage = 1;
  144. }
  145. else if (sender == btnPrev)
  146. {
  147. if (this.CurrentPage > 1)
  148. this.CurrentPage--;
  149. }
  150. else if (sender == btnNext)
  151. {
  152. this.CurrentPage++;
  153. }
  154. else if (sender == btnLast)
  155. {
  156. this.CurrentPage = int.MaxValue;
  157. }
  158. else
  159. {
  160. int.TryParse(txtGoPage.Text, out int currentPage);
  161. if (currentPage <= 0) return;
  162. this.CurrentPage = currentPage;
  163. }
  164. int.TryParse(txtCountPerPage.Text, out int countPerPage);
  165. if (countPerPage <= 0)
  166. {
  167. countPerPage = (int)txtCountPerPage.Properties.Items[0];
  168. }
  169. this.Enabled = false;
  170. var pageData = await this.OnQueryAsync(new PageQueryDto()
  171. {
  172. CountPerPage = countPerPage,
  173. Page = CurrentPage,
  174. });
  175. if (pageData == null)
  176. {
  177. pageData = new DTO.PageData()
  178. {
  179. CountPerPage = 100,
  180. Page = 1,
  181. TotalCount = 0,
  182. };
  183. }
  184. this.CurrentPage = pageData.Page;
  185. txtPageInfo.Text = $"第{pageData.Page}页/共{pageData.TotalPage}页";
  186. }
  187. catch (Exception ex)
  188. {
  189. IocContainer.Logger.Error(ex, $"分页查询异常");
  190. }
  191. finally
  192. {
  193. this.Enabled = true;
  194. }
  195. }
  196. }
  197. }