123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using System;
- using System.Data;
- using System.IO;
- using System.Windows.Forms;
- using UAS_LabelMachine.Entity;
- namespace UAS_LabelMachine.CustomControl
- {
- public partial class Pagination : UserControl
- {
- //申明一些全局变量用来在函数之间传递参数
- int PageSize;
- int Current_Page;
- int TotalPageCount;
- int RowCount;
- string Table_ID;
- string TableName;
- string Caller;
- string Field;
- DataGridViewImageColumn dgic = null;
- string Condition;
- DataGridView Dgv;
- DataTable dt;
- DataHelper dh;
- public Pagination()
- {
- InitializeComponent();
- //鼠标经过控件的时候显示文字
- dh = SystemInf.dh;
- toolTip1.SetToolTip(DownLoadTemplet, "下载模板");
- toolTip1.SetToolTip(UpLoadData, "上传数据");
- toolTip1.SetToolTip(ExportExcel, "导出数据");
- toolTip1.SetToolTip(Delete, "删除数据");
- toolTip1.SetToolTip(FirstPage, "第一页");
- toolTip1.SetToolTip(PreviousPage, "上一页");
- toolTip1.SetToolTip(NextPage, "下一页");
- toolTip1.SetToolTip(LastPage, "最后一页");
- }
- public delegate void OnCheckedChange(object sender, EventArgs e);
- //定义事件
- public event OnCheckedChange UserControlCheckedChanged;
- /// <summary>
- /// 将数据绑定到数据源
- /// </summary>
- /// <param name="dgv"></param>
- /// <param name="TableName"></param>
- /// <param name="Field"></param>
- public void BindDataToNavigator(DataGridView dgv, string tableName, string field, string ID, string caller, string condition, params DataGridViewImageColumn[] operate)
- {
- //将传来的值赋给全局变量
- Dgv = dgv;
- TableName = tableName;
- Table_ID = ID;
- Caller = caller;
- Field = field;
- Condition = condition;
- //每页显示的数据数量
- PageSize = int.Parse(this.ShowItemsNum.Text);
- //当前在多少页
- Current_Page = int.Parse(this.CurrentPage.Text);
- //获取记录的总行数
- RowCount = dh.getRowCount(TableName);
- //分页获取数据,先取第一页的
- dt = dh.getFieldsDatasByPageing(TableName, field, Current_Page, PageSize, Caller, condition);
- //给BindingSource指定一个DataTable
- RefreshTotalCount();
- //给父控件的DataGridView绑定数据
- dgv.DataSource = dt;
- dt.Dispose();
- for (int i = 0; i < operate.Length; i++)
- {
- dgic = operate[i];
- AddOperateColumnToDgv();
- }
- }
- //获取分页的数据
- public void GetPageData()
- {
- this.TotalPage.Text = "/ " + TotalPageCount.ToString();
- CurrentPage.Text = Current_Page.ToString();
- //取分页数据
- dt = dh.getFieldsDatasByPageing(TableName, Field, Current_Page, PageSize, Caller, Condition);
- //赋值给父窗体的DataGridView
- Dgv.DataSource = dt;
- AddOperateColumnToDgv();
- }
- //判断是否需要添加操作列
- private void AddOperateColumnToDgv()
- {
- if (dgic != null)
- {
- if (Dgv.Columns[dgic.Name] != null)
- {
- Dgv.Columns.Remove(Dgv.Columns[dgic.Name]);
- }
- dgic.DisplayIndex = Dgv.Columns.Count;
- Dgv.Columns.Add(dgic);
- }
- }
- //第一页
- private void FirstPage_Click(object sender, EventArgs e)
- {
- Current_Page = 1;
- GetPageData();
- RefreshTotalCount();
- }
- //往前翻一页
- private void PreviousPage_Click(object sender, EventArgs e)
- {
- if (Current_Page > 1)
- {
- Current_Page--;
- }
- GetPageData();
- RefreshTotalCount();
- }
- //往后翻一页
- private void NextPage_Click(object sender, EventArgs e)
- {
- if (Current_Page < TotalPageCount)
- {
- Current_Page++;
- }
- GetPageData();
- RefreshTotalCount();
- }
- //最后一页
- private void LastPage_Click(object sender, EventArgs e)
- {
- Current_Page = TotalPageCount;
- CurrentPage.Text = TotalPageCount.ToString();
- GetPageData();
- RefreshTotalCount();
- }
- private void ExportExcel_Click(object sender, EventArgs e)
- {
- ExcelExport("");
- }
- //用户手动输入页码的时候获取数据
- private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
- {
- //判断用户是否按下回车键
- if (e.KeyValue == (int)Keys.Enter)
- {
- //判断是否是数字类型的
- Current_Page = int.Parse(CurrentPage.Text);
- //判断是否是合理的页码
- if (Current_Page >= 1 && Current_Page <= TotalPageCount)
- {
- GetPageData();
- }
- else
- {
- MessageBox.Show("请输入正确的页码");
- }
- }
- }
- //刷新总页数
- private void RefreshTotalCount()
- {
- DataHelper dh = SystemInf.dh;
- //获取记录的总行数
- if (Condition.Trim() != "")
- {
- RowCount = dh.getRowCount(TableName, Condition);
- }
- else
- {
- RowCount = dh.getRowCount(TableName);
- }
- //取模后+1来确定总的页数
- TotalPageCount = RowCount / PageSize + 1;
- //设置总页数Label显示的值
- TotalPage.Text = "/ " + TotalPageCount.ToString();
- }
- //删除按钮
- private void Delete_Click(object sender, EventArgs e)
- {
- UserControlCheckedChanged?.Invoke(sender, new EventArgs());
- for (int i = 0; i < Dgv.Rows.Count; i++)
- {
- if (Dgv.Rows[i].Cells["Choose"].Value != null)
- {
- if ((bool)Dgv.Rows[i].Cells["Choose"].Value == true)
- {
- Dgv.Rows[i].Selected = true;
- }
- }
- }
- string[] DeleteID = new string[Dgv.SelectedRows.Count];
- DataTable dt = (DataTable)Dgv.DataSource;
- string getIDColumnName = "";
- //取列的描述,对应的主键的ID
- foreach (DataColumn dc in dt.Columns)
- {
- if (dc.ColumnName == Table_ID)
- {
- getIDColumnName = dc.Caption;
- }
- }
- for (int i = 0; i < Dgv.SelectedRows.Count; i++)
- {
- DeleteID[i] = Dgv.SelectedRows[i].Cells[Table_ID].Value.ToString();
- }
- DataHelper dh = SystemInf.dh;
- //取的配置的数据是从DataTable里面判断的,取的是固定字段的Dgv用第二种
- string tablename = TableName;
- if (TableName.Contains("join"))
- {
- tablename = TableName.Substring(0, TableName.IndexOf("left")).Trim();
- }
- dh.DeleteDataByID(tablename, getIDColumnName == "" ? Table_ID : getIDColumnName, DeleteID);
- GetPageData();
- RefreshTotalCount();
- }
- //添加新的数据
- private void Add_Click(object sender, EventArgs e)
- {
- ((DataTable)Dgv.DataSource).Rows.Add();
- }
- private void DownLoadTemplet_Click(object sender, EventArgs e)
- {
- ExcelExport("Templet");
- }
- //选择导出Excel时是选择导出数据的还是模板
- private void ExcelExport(string DataOrTemplet)
- {
- //Data表示导出数据
- //Templet表示导出模板
- DataHelper dh = SystemInf.dh;
- folderBrowserDialog1.Description = "选择导出的路径";
- DialogResult result = folderBrowserDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- string FolderPath = folderBrowserDialog1.SelectedPath;
- ExcelHandler eh = new ExcelHandler();
- //导出Excel的时候返回一个文件名,用户选择是否打开
- string filePath = eh.ExportExcel(BaseUtil.GetExportDataTable(Dgv), FolderPath, "客户标签");
- //用户选择导出之后是否立即打开
- MessageBoxButtons messButton = MessageBoxButtons.YesNo;
- string openFile = MessageBox.Show(this.ParentForm, "是否打开文件", "提示", messButton).ToString();
- if (openFile == "Yes")
- {
- System.Diagnostics.Process.Start(filePath);
- }
- }
- }
- private void UpLoadData_Click(object sender, EventArgs e)
- {
- openFileDialog1.Title = "选择需要导入的Excel";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string ChoosedExcel = openFileDialog1.FileName;
- FileStream fs = new FileStream(ChoosedExcel, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- }
- }
- private void ShowItemsNum_SelectedValueChanged(object sender, EventArgs e)
- {
- PageSize = int.Parse(ShowItemsNum.SelectedItem.ToString());
- //推荐合理的行数
- //int AdviceNum = DgvHeight / DgvRowHeight - 3;
- if (PageSize >= 1)
- {
- RefreshTotalCount();
- GetPageData();
- }
- else
- {
- MessageBox.Show("请输入正确的数字");
- }
- }
- private void ShowItemsNum_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- PageSize = int.Parse(ShowItemsNum.Text);
- if (PageSize >= 1)
- {
- RefreshTotalCount();
- GetPageData();
- }
- else
- {
- MessageBox.Show("请输入正确的数字");
- }
- }
- }
- }
- }
|