CtrlPage.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /// <summary>
  89. /// 当前页数
  90. /// </summary>
  91. private int CurrentPage { get; set; } = 1;
  92. public CtrlPage()
  93. {
  94. InitializeComponent();
  95. this.MinimumSize = new System.Drawing.Size(100, 26);
  96. }
  97. /// <summary>
  98. /// 刷新数据(当前页会变为1)
  99. /// </summary>
  100. public void RefreshData()
  101. {
  102. btn_Click(btnFirst, null);
  103. }
  104. private void txtCountPerPage_EditValueChanged(object sender, EventArgs e)
  105. {
  106. btn_Click(btnFirst, null);
  107. }
  108. private async void btn_Click(object sender, EventArgs e)
  109. {
  110. try
  111. {
  112. if (OnQueryAsync == null) return;
  113. if (sender == btnFirst)
  114. {
  115. this.CurrentPage = 1;
  116. }
  117. else if (sender == btnPrev)
  118. {
  119. if (this.CurrentPage > 1)
  120. this.CurrentPage--;
  121. }
  122. else if (sender == btnNext)
  123. {
  124. this.CurrentPage++;
  125. }
  126. else if (sender == btnLast)
  127. {
  128. this.CurrentPage = int.MaxValue;
  129. }
  130. else
  131. {
  132. int.TryParse(txtGoPage.Text, out int currentPage);
  133. if (currentPage <= 0) return;
  134. this.CurrentPage = currentPage;
  135. }
  136. int.TryParse(txtCountPerPage.Text, out int countPerPage);
  137. if (countPerPage <= 0)
  138. {
  139. countPerPage = (int)txtCountPerPage.Properties.Items[0];
  140. }
  141. this.Enabled = false;
  142. var pageData = await this.OnQueryAsync(new PageQueryDto()
  143. {
  144. CountPerPage = countPerPage,
  145. Page = CurrentPage,
  146. });
  147. this.CurrentPage = pageData.Page;
  148. txtPageInfo.Text = $"第{pageData.Page}页/共{pageData.TotalPage}页";
  149. }
  150. catch (Exception ex)
  151. {
  152. IocContainer.Logger.Error(ex, $"分页查询异常");
  153. }
  154. finally
  155. {
  156. this.Enabled = true;
  157. }
  158. }
  159. }
  160. }