Pagination.cs 10 KB

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