PaginationDbFind.cs 13 KB

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