Pagination.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using UAS_LabelMachine.Entity;
  6. namespace UAS_LabelMachine.CustomControl
  7. {
  8. public partial class Pagination : UserControl
  9. {
  10. //申明一些全局变量用来在函数之间传递参数
  11. int PageSize;
  12. int Current_Page;
  13. int TotalPageCount;
  14. int RowCount;
  15. string Table_ID;
  16. string TableName;
  17. string Caller;
  18. string Field;
  19. DataGridViewImageColumn dgic = null;
  20. string Condition;
  21. DataGridView Dgv;
  22. DataTable dt;
  23. DataHelper dh;
  24. public Pagination()
  25. {
  26. InitializeComponent();
  27. //鼠标经过控件的时候显示文字
  28. dh = SystemInf.dh;
  29. toolTip1.SetToolTip(DownLoadTemplet, "下载模板");
  30. toolTip1.SetToolTip(UpLoadData, "上传数据");
  31. toolTip1.SetToolTip(ExportExcel, "导出数据");
  32. toolTip1.SetToolTip(Delete, "删除数据");
  33. toolTip1.SetToolTip(FirstPage, "第一页");
  34. toolTip1.SetToolTip(PreviousPage, "上一页");
  35. toolTip1.SetToolTip(NextPage, "下一页");
  36. toolTip1.SetToolTip(LastPage, "最后一页");
  37. }
  38. public delegate void OnCheckedChange(object sender, EventArgs e);
  39. //定义事件
  40. public event OnCheckedChange UserControlCheckedChanged;
  41. /// <summary>
  42. /// 将数据绑定到数据源
  43. /// </summary>
  44. /// <param name="dgv"></param>
  45. /// <param name="TableName"></param>
  46. /// <param name="Field"></param>
  47. public void BindDataToNavigator(DataGridView dgv, string tableName, string field, string ID, string caller, string condition, params DataGridViewImageColumn[] operate)
  48. {
  49. //将传来的值赋给全局变量
  50. Dgv = dgv;
  51. TableName = tableName;
  52. Table_ID = ID;
  53. Caller = caller;
  54. Field = field;
  55. Condition = condition;
  56. //每页显示的数据数量
  57. PageSize = int.Parse(this.ShowItemsNum.Text);
  58. //当前在多少页
  59. Current_Page = int.Parse(this.CurrentPage.Text);
  60. //获取记录的总行数
  61. RowCount = dh.getRowCount(TableName);
  62. //分页获取数据,先取第一页的
  63. dt = dh.getFieldsDatasByPageing(TableName, field, Current_Page, PageSize, Caller, condition);
  64. //给BindingSource指定一个DataTable
  65. RefreshTotalCount();
  66. //给父控件的DataGridView绑定数据
  67. dgv.DataSource = dt;
  68. dt.Dispose();
  69. for (int i = 0; i < operate.Length; i++)
  70. {
  71. dgic = operate[i];
  72. AddOperateColumnToDgv();
  73. }
  74. }
  75. //获取分页的数据
  76. public void GetPageData()
  77. {
  78. this.TotalPage.Text = "/ " + TotalPageCount.ToString();
  79. CurrentPage.Text = Current_Page.ToString();
  80. //取分页数据
  81. dt = dh.getFieldsDatasByPageing(TableName, Field, Current_Page, PageSize, Caller, Condition);
  82. //赋值给父窗体的DataGridView
  83. Dgv.DataSource = dt;
  84. AddOperateColumnToDgv();
  85. }
  86. //判断是否需要添加操作列
  87. private void AddOperateColumnToDgv()
  88. {
  89. if (dgic != null)
  90. {
  91. if (Dgv.Columns[dgic.Name] != null)
  92. {
  93. Dgv.Columns.Remove(Dgv.Columns[dgic.Name]);
  94. }
  95. dgic.DisplayIndex = Dgv.Columns.Count;
  96. Dgv.Columns.Add(dgic);
  97. }
  98. }
  99. //第一页
  100. private void FirstPage_Click(object sender, EventArgs e)
  101. {
  102. Current_Page = 1;
  103. GetPageData();
  104. RefreshTotalCount();
  105. }
  106. //往前翻一页
  107. private void PreviousPage_Click(object sender, EventArgs e)
  108. {
  109. if (Current_Page > 1)
  110. {
  111. Current_Page--;
  112. }
  113. GetPageData();
  114. RefreshTotalCount();
  115. }
  116. //往后翻一页
  117. private void NextPage_Click(object sender, EventArgs e)
  118. {
  119. if (Current_Page < TotalPageCount)
  120. {
  121. Current_Page++;
  122. }
  123. GetPageData();
  124. RefreshTotalCount();
  125. }
  126. //最后一页
  127. private void LastPage_Click(object sender, EventArgs e)
  128. {
  129. Current_Page = TotalPageCount;
  130. CurrentPage.Text = TotalPageCount.ToString();
  131. GetPageData();
  132. RefreshTotalCount();
  133. }
  134. private void ExportExcel_Click(object sender, EventArgs e)
  135. {
  136. ExcelExport("");
  137. }
  138. //用户手动输入页码的时候获取数据
  139. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  140. {
  141. //判断用户是否按下回车键
  142. if (e.KeyValue == (int)Keys.Enter)
  143. {
  144. //判断是否是数字类型的
  145. Current_Page = int.Parse(CurrentPage.Text);
  146. //判断是否是合理的页码
  147. if (Current_Page >= 1 && Current_Page <= TotalPageCount)
  148. {
  149. GetPageData();
  150. }
  151. else
  152. {
  153. MessageBox.Show("请输入正确的页码");
  154. }
  155. }
  156. }
  157. //刷新总页数
  158. private void RefreshTotalCount()
  159. {
  160. DataHelper dh =SystemInf.dh;
  161. //获取记录的总行数
  162. if (Condition.Trim() != "")
  163. {
  164. RowCount = dh.getRowCount(TableName, Condition);
  165. }
  166. else
  167. {
  168. RowCount = dh.getRowCount(TableName);
  169. }
  170. //取模后+1来确定总的页数
  171. TotalPageCount = RowCount / PageSize + 1;
  172. //设置总页数Label显示的值
  173. TotalPage.Text = "/ " + TotalPageCount.ToString();
  174. }
  175. //删除按钮
  176. private void Delete_Click(object sender, EventArgs e)
  177. {
  178. UserControlCheckedChanged?.Invoke(sender, new EventArgs());
  179. for (int i = 0; i < Dgv.Rows.Count; i++)
  180. {
  181. if (Dgv.Rows[i].Cells["Choose"].Value != null)
  182. {
  183. if ((bool)Dgv.Rows[i].Cells["Choose"].Value == true)
  184. {
  185. Dgv.Rows[i].Selected = true;
  186. }
  187. }
  188. }
  189. string[] DeleteID = new string[Dgv.SelectedRows.Count];
  190. DataTable dt = (DataTable)Dgv.DataSource;
  191. string getIDColumnName = "";
  192. //取列的描述,对应的主键的ID
  193. foreach (DataColumn dc in dt.Columns)
  194. {
  195. if (dc.ColumnName == Table_ID)
  196. {
  197. getIDColumnName = dc.Caption;
  198. }
  199. }
  200. for (int i = 0; i < Dgv.SelectedRows.Count; i++)
  201. {
  202. DeleteID[i] = Dgv.SelectedRows[i].Cells[Table_ID].Value.ToString();
  203. }
  204. DataHelper dh = SystemInf.dh;
  205. //取的配置的数据是从DataTable里面判断的,取的是固定字段的Dgv用第二种
  206. string tablename = TableName;
  207. if (TableName.Contains("join"))
  208. {
  209. tablename = TableName.Substring(0, TableName.IndexOf("left")).Trim();
  210. }
  211. dh.DeleteDataByID(tablename, getIDColumnName == "" ? Table_ID : getIDColumnName, DeleteID);
  212. GetPageData();
  213. RefreshTotalCount();
  214. }
  215. //添加新的数据
  216. private void Add_Click(object sender, EventArgs e)
  217. {
  218. ((DataTable)Dgv.DataSource).Rows.Add();
  219. }
  220. private void DownLoadTemplet_Click(object sender, EventArgs e)
  221. {
  222. ExcelExport("Templet");
  223. }
  224. //选择导出Excel时是选择导出数据的还是模板
  225. private void ExcelExport(string DataOrTemplet)
  226. {
  227. //Data表示导出数据
  228. //Templet表示导出模板
  229. DataHelper dh = SystemInf.dh;
  230. folderBrowserDialog1.Description = "选择导出的路径";
  231. DialogResult result = folderBrowserDialog1.ShowDialog();
  232. if (result == DialogResult.OK)
  233. {
  234. string FolderPath = folderBrowserDialog1.SelectedPath;
  235. ExcelHandler eh = new ExcelHandler();
  236. //导出Excel的时候返回一个文件名,用户选择是否打开
  237. string filePath = eh.ExportExcel_Normal(BaseUtil.GetExportDataTable(Dgv), BaseUtil.GetExportDataTable(Dgv), FolderPath, "客户标签","",100,null);
  238. //用户选择导出之后是否立即打开
  239. MessageBoxButtons messButton = MessageBoxButtons.YesNo;
  240. string openFile = MessageBox.Show(this.ParentForm, "是否打开文件", "提示", messButton).ToString();
  241. if (openFile == "Yes")
  242. {
  243. System.Diagnostics.Process.Start(filePath);
  244. }
  245. }
  246. }
  247. private void UpLoadData_Click(object sender, EventArgs e)
  248. {
  249. openFileDialog1.Title = "选择需要导入的Excel";
  250. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  251. {
  252. string ChoosedExcel = openFileDialog1.FileName;
  253. FileStream fs = new FileStream(ChoosedExcel, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  254. }
  255. }
  256. private void ShowItemsNum_SelectedValueChanged(object sender, EventArgs e)
  257. {
  258. PageSize = int.Parse(ShowItemsNum.SelectedItem.ToString());
  259. //推荐合理的行数
  260. //int AdviceNum = DgvHeight / DgvRowHeight - 3;
  261. if (PageSize >= 1)
  262. {
  263. RefreshTotalCount();
  264. GetPageData();
  265. }
  266. else
  267. {
  268. MessageBox.Show("请输入正确的数字");
  269. }
  270. }
  271. private void ShowItemsNum_KeyDown(object sender, KeyEventArgs e)
  272. {
  273. if (e.KeyCode == Keys.Enter)
  274. {
  275. PageSize = int.Parse(ShowItemsNum.Text);
  276. if (PageSize >= 1)
  277. {
  278. RefreshTotalCount();
  279. GetPageData();
  280. }
  281. else
  282. {
  283. MessageBox.Show("请输入正确的数字");
  284. }
  285. }
  286. }
  287. }
  288. }