Pagination.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Windows.Forms;
  5. namespace UAS_BARCODEIO
  6. {
  7. public partial class Pagination : UserControl
  8. {
  9. #region 变量
  10. //申明一些全局变量用来在函数之间传递参数
  11. int PageSize;
  12. /// <summary>
  13. /// 当前页码
  14. /// </summary>
  15. public int Current_Page;
  16. /// <summary>
  17. /// 总页数
  18. /// </summary>
  19. int TotalPageCount;
  20. /// <summary>
  21. /// 一页展示的行数
  22. /// </summary>
  23. int RowCount;
  24. /// <summary>
  25. /// 主表的ID
  26. /// </summary>
  27. string Table_ID;
  28. /// <summary>
  29. /// 查询的表名
  30. /// </summary>
  31. string TableName;
  32. string Caller;
  33. /// <summary>
  34. /// 查询的字段
  35. /// </summary>
  36. string Field;
  37. /// <summary>
  38. /// 需要添加的图像操作列
  39. /// </summary>
  40. DataGridViewImageColumn dgic = null;
  41. string Condition = "";
  42. /// <summary>
  43. /// 删除按钮是否能够点击
  44. /// </summary>
  45. bool DeleteEnable1;
  46. /// <summary>
  47. /// 添加按钮是否能够点击
  48. /// </summary>
  49. bool AddEnable1;
  50. DataGridView Dgv;
  51. DataTable dt;
  52. DataHelper dh = SystemInf.dh;
  53. public bool DeleteEnable
  54. {
  55. get
  56. {
  57. return DeleteEnable1;
  58. }
  59. set
  60. {
  61. DeleteEnable1 = value;
  62. }
  63. }
  64. public bool AddEnable
  65. {
  66. get
  67. {
  68. return AddEnable1;
  69. }
  70. set
  71. {
  72. AddEnable1 = value;
  73. }
  74. }
  75. #endregion
  76. public Pagination()
  77. {
  78. InitializeComponent();
  79. //鼠标经过控件的时候显示文字
  80. toolTip1.SetToolTip(DownLoadTemplet, "下载模板");
  81. toolTip1.SetToolTip(UpLoadData, "上传数据");
  82. toolTip1.SetToolTip(ExportExcel, "导出数据");
  83. toolTip1.SetToolTip(Delete, "删除数据");
  84. toolTip1.SetToolTip(FirstPage, "第一页");
  85. toolTip1.SetToolTip(PreviousPage, "上一页");
  86. toolTip1.SetToolTip(NextPage, "下一页");
  87. toolTip1.SetToolTip(LastPage, "最后一页");
  88. }
  89. public delegate void OnCheckedChange(object sender, EventArgs e);
  90. //定义事件
  91. public event OnCheckedChange UserControlCheckedChanged;
  92. /// <summary>
  93. /// 将数据绑定到数据源
  94. /// </summary>
  95. /// <param name="dgv"></param>
  96. /// <param name="TableName"></param>
  97. /// <param name="Field"></param>
  98. public void BindDataToNavigator(DataGridView dgv, string tableName, string field, string ID, string caller, string condition, params DataGridViewImageColumn[] operate)
  99. {
  100. //将传来的值赋给全局变量
  101. Dgv = dgv;
  102. TableName = tableName;
  103. Table_ID = ID;
  104. Caller = caller;
  105. Field = field;
  106. Condition = condition;
  107. //每页显示的数据数量
  108. PageSize = int.Parse(this.ShowItemsNum.Text);
  109. //当前在多少页
  110. Current_Page = int.Parse(this.CurrentPage.Text);
  111. //获取记录的总行数
  112. RowCount = dh.getRowCount(TableName);
  113. //分页获取数据,先取第一页的
  114. dt = dh.getFieldsDatasByPageing(TableName, field, Current_Page, PageSize, Caller, condition);
  115. //给BindingSource指定一个DataTable
  116. RefreshTotalCount();
  117. //给父控件的DataGridView绑定数据
  118. dgv.DataSource = dt;
  119. dt.Dispose();
  120. for (int i = 0; i < operate.Length; i++)
  121. {
  122. dgic = operate[i];
  123. AddOperateColumnToDgv();
  124. }
  125. }
  126. //获取分页的数据
  127. public void GetPageData()
  128. {
  129. this.TotalPage.Text = "/ " + TotalPageCount.ToString();
  130. CurrentPage.Text = Current_Page.ToString();
  131. //取分页数据
  132. dt = dh.getFieldsDatasByPageing(TableName, Field, Current_Page, PageSize, Caller, Condition);
  133. //赋值给父窗体的DataGridView
  134. Dgv.DataSource = dt;
  135. AddOperateColumnToDgv();
  136. }
  137. //判断是否需要添加操作列
  138. private void AddOperateColumnToDgv()
  139. {
  140. if (dgic != null)
  141. {
  142. if (Dgv.Columns[dgic.Name] != null)
  143. {
  144. Dgv.Columns.Remove(Dgv.Columns[dgic.Name]);
  145. }
  146. dgic.DisplayIndex = Dgv.Columns.Count;
  147. Dgv.Columns.Add(dgic);
  148. }
  149. }
  150. //第一页
  151. private void FirstPage_Click(object sender, EventArgs e)
  152. {
  153. Current_Page = 1;
  154. GetPageData();
  155. RefreshTotalCount();
  156. }
  157. //往前翻一页
  158. private void PreviousPage_Click(object sender, EventArgs e)
  159. {
  160. if (Current_Page > 1)
  161. {
  162. Current_Page--;
  163. }
  164. GetPageData();
  165. RefreshTotalCount();
  166. }
  167. //往后翻一页
  168. private void NextPage_Click(object sender, EventArgs e)
  169. {
  170. if (Current_Page < TotalPageCount)
  171. {
  172. Current_Page++;
  173. }
  174. GetPageData();
  175. RefreshTotalCount();
  176. }
  177. //最后一页
  178. private void LastPage_Click(object sender, EventArgs e)
  179. {
  180. Current_Page = TotalPageCount;
  181. CurrentPage.Text = TotalPageCount.ToString();
  182. GetPageData();
  183. RefreshTotalCount();
  184. }
  185. private void ExportExcel_Click(object sender, EventArgs e)
  186. {
  187. ExcelExport("Data");
  188. }
  189. //用户手动输入页码的时候获取数据
  190. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  191. {
  192. //判断用户是否按下回车键
  193. if (e.KeyValue == (int)Keys.Enter)
  194. {
  195. //判断是否是数字类型的
  196. if (IsNumOrNull(CurrentPage.Text))
  197. {
  198. Current_Page = int.Parse(CurrentPage.Text);
  199. //判断是否是合理的页码
  200. if (Current_Page >= 1 && Current_Page <= TotalPageCount)
  201. {
  202. GetPageData();
  203. }
  204. else
  205. {
  206. MessageBox.Show("请输入正确的页码");
  207. }
  208. }
  209. else
  210. {
  211. CurrentPage.Text = "1";
  212. MessageBox.Show("页码必须是不为空的正整数");
  213. }
  214. }
  215. }
  216. //判断是不是数字或者空值
  217. private bool IsNumOrNull(object Num)
  218. {
  219. //判断是否是数组
  220. foreach (char c in Num.ToString())
  221. {
  222. if (!char.IsDigit(c))
  223. {
  224. return false;
  225. }
  226. }
  227. //判断是否为空值
  228. if (Num.ToString() == "")
  229. {
  230. return false;
  231. }
  232. return true;
  233. }
  234. //刷新总页数
  235. private void RefreshTotalCount()
  236. {
  237. //获取记录的总行数
  238. if (Condition.Trim() != "")
  239. {
  240. RowCount = dh.getRowCount(TableName, Condition);
  241. }
  242. else
  243. {
  244. RowCount = dh.getRowCount(TableName);
  245. }
  246. //取模后+1来确定总的页数
  247. TotalPageCount = RowCount / PageSize + 1;
  248. //设置总页数Label显示的值
  249. TotalPage.Text = "/ " + TotalPageCount.ToString();
  250. }
  251. //删除按钮
  252. private void Delete_Click(object sender, EventArgs e)
  253. {
  254. //CheckBox需要失去焦点的时候值才变化,手动使焦点到按钮这
  255. Delete.Focus();
  256. UserControlCheckedChanged?.Invoke(sender, new EventArgs());
  257. {
  258. List<string> DeleteID = new List<string>();
  259. for (int i = 0; i < Dgv.Rows.Count; i++)
  260. {
  261. if (Dgv.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
  262. {
  263. DeleteID.Add(Dgv.Rows[i].Cells[Table_ID].FormattedValue.ToString());
  264. }
  265. }
  266. if (DeleteID.ToArray().Length > 0)
  267. {
  268. //取的配置的数据是从DataTable里面判断的,取的是固定字段的Dgv用第二种
  269. string tablename = TableName;
  270. if (TableName.Contains("join"))
  271. {
  272. tablename = TableName.Substring(0, TableName.IndexOf("left")).Trim();
  273. }
  274. dh.DeleteDataByID(tablename, Table_ID, DeleteID.ToArray());
  275. GetPageData();
  276. RefreshTotalCount();
  277. }
  278. }
  279. }
  280. //添加新的数据
  281. private void Add_Click(object sender, EventArgs e)
  282. {
  283. ((DataTable)Dgv.DataSource).Rows.Add();
  284. }
  285. private void DownLoadTemplet_Click(object sender, EventArgs e)
  286. {
  287. ExcelExport("Templet");
  288. }
  289. //选择导出Excel时是选择导出数据的还是模板
  290. private void ExcelExport(string DataOrTemplet)
  291. {
  292. }
  293. private void UpLoadData_Click(object sender, EventArgs e)
  294. {
  295. }
  296. private void ShowItemsNum_SelectedValueChanged(object sender, EventArgs e)
  297. {
  298. if (IsNumOrNull(ShowItemsNum.SelectedItem))
  299. {
  300. PageSize = int.Parse(ShowItemsNum.SelectedItem.ToString());
  301. //推荐合理的行数
  302. //int AdviceNum = DgvHeight / DgvRowHeight - 3;
  303. if (PageSize >= 1)
  304. {
  305. RefreshTotalCount();
  306. GetPageData();
  307. }
  308. else
  309. {
  310. MessageBox.Show("请输入正确的数字");
  311. }
  312. }
  313. else
  314. {
  315. MessageBox.Show("页码必须是不为空的正整数");
  316. }
  317. }
  318. private void Pagination_Load(object sender, EventArgs e)
  319. {
  320. Delete.Enabled = DeleteEnable1;
  321. Add.Enabled = AddEnable1;
  322. }
  323. }
  324. }