123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Windows.Forms;
- namespace FileWatcher
- {
- public partial class Pagination : UserControl
- {
- #region 变量
- //申明一些全局变量用来在函数之间传递参数
- int PageSize;
- /// <summary>
- /// 当前页码
- /// </summary>
- public int Current_Page;
- /// <summary>
- /// 总页数
- /// </summary>
- int TotalPageCount;
- /// <summary>
- /// 一页展示的行数
- /// </summary>
- int RowCount;
- /// <summary>
- /// 主表的ID
- /// </summary>
- string Table_ID;
- /// <summary>
- /// 查询的表名
- /// </summary>
- string TableName;
- string Caller;
- /// <summary>
- /// 查询的字段
- /// </summary>
- string Field;
- /// <summary>
- /// 需要添加的图像操作列
- /// </summary>
- DataGridViewImageColumn dgic = null;
- string Condition = "";
- /// <summary>
- /// 删除按钮是否能够点击
- /// </summary>
- bool DeleteEnable1;
- /// <summary>
- /// 添加按钮是否能够点击
- /// </summary>
- bool AddEnable1;
- DataGridView Dgv;
- DataTable dt;
- DataHelper dh = SystemInf.dh;
- public bool DeleteEnable
- {
- get
- {
- return DeleteEnable1;
- }
- set
- {
- DeleteEnable1 = value;
- }
- }
- public bool AddEnable
- {
- get
- {
- return AddEnable1;
- }
- set
- {
- AddEnable1 = value;
- }
- }
- #endregion
- public Pagination()
- {
- InitializeComponent();
- //鼠标经过控件的时候显示文字
- 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("Data");
- }
- //用户手动输入页码的时候获取数据
- private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
- {
- //判断用户是否按下回车键
- if (e.KeyValue == (int)Keys.Enter)
- {
- //判断是否是数字类型的
- if (IsNumOrNull(CurrentPage.Text))
- {
- Current_Page = int.Parse(CurrentPage.Text);
- //判断是否是合理的页码
- if (Current_Page >= 1 && Current_Page <= TotalPageCount)
- {
- GetPageData();
- }
- else
- {
- MessageBox.Show("请输入正确的页码");
- }
- }
- else
- {
- CurrentPage.Text = "1";
- MessageBox.Show("页码必须是不为空的正整数");
- }
- }
- }
- //判断是不是数字或者空值
- private bool IsNumOrNull(object Num)
- {
- //判断是否是数组
- foreach (char c in Num.ToString())
- {
- if (!char.IsDigit(c))
- {
- return false;
- }
- }
- //判断是否为空值
- if (Num.ToString() == "")
- {
- return false;
- }
- return true;
- }
- //刷新总页数
- private void RefreshTotalCount()
- {
- //获取记录的总行数
- 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)
- {
- //CheckBox需要失去焦点的时候值才变化,手动使焦点到按钮这
- Delete.Focus();
- UserControlCheckedChanged?.Invoke(sender, new EventArgs());
- {
- List<string> DeleteID = new List<string>();
- for (int i = 0; i < Dgv.Rows.Count; i++)
- {
- if (Dgv.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
- {
- DeleteID.Add(Dgv.Rows[i].Cells[Table_ID].FormattedValue.ToString());
- }
- }
- if (DeleteID.ToArray().Length > 0)
- {
- //取的配置的数据是从DataTable里面判断的,取的是固定字段的Dgv用第二种
- string tablename = TableName;
- if (TableName.Contains("join"))
- {
- tablename = TableName.Substring(0, TableName.IndexOf("left")).Trim();
- }
- dh.DeleteDataByID(tablename, Table_ID, DeleteID.ToArray());
- 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)
- {
-
- }
- private void UpLoadData_Click(object sender, EventArgs e)
- {
-
- }
- private void ShowItemsNum_SelectedValueChanged(object sender, EventArgs e)
- {
- if (IsNumOrNull(ShowItemsNum.SelectedItem))
- {
- PageSize = int.Parse(ShowItemsNum.SelectedItem.ToString());
- //推荐合理的行数
- //int AdviceNum = DgvHeight / DgvRowHeight - 3;
- if (PageSize >= 1)
- {
- RefreshTotalCount();
- GetPageData();
- }
- else
- {
- MessageBox.Show("请输入正确的数字");
- }
- }
- else
- {
- MessageBox.Show("页码必须是不为空的正整数");
- }
- }
- private void Pagination_Load(object sender, EventArgs e)
- {
- Delete.Enabled = DeleteEnable1;
- Add.Enabled = AddEnable1;
- }
- }
- }
|