using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
namespace UAS_MES_NEW.CustomControl.Pagination
{
public partial class PaginationDbFind : UserControl
{
#region 变量
//申明一些全局变量用来在函数之间传递参数
int PageSize;
///
/// 当前页码
///
public int Current_Page;
///
/// 总页数
///
int TotalPageCount;
///
/// 一页展示的行数
///
int RowCount;
///
/// 主表的ID
///
string Table_ID;
///
/// 查询的表名
///
string TableName;
string Caller;
///
/// 查询的字段
///
string Field;
///
/// 需要添加的图像操作列
///
DataGridViewImageColumn dgic = null;
string Condition = "";
///
/// 删除按钮是否能够点击
///
bool DeleteEnable1;
///
/// 添加按钮是否能够点击
///
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 PaginationDbFind()
{
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;
///
/// 将数据绑定到数据源
///
///
///
///
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 DeleteID = new List();
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)
{
//Data表示导出数据
//Templet表示导出模板
folderBrowserDialog1.Description = "选择导出的路径";
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string FolderPath = folderBrowserDialog1.SelectedPath;
ExcelHandler eh = new ExcelHandler();
DataTable dt = new DataTable();
if (DataOrTemplet == "Data")
{
dt = dh.getFieldsDatas(TableName, Field);
}
else
{
//这是根据表名获取到的列和列的数据类型,是column的类型,需要手动转换成row类型
dt = dh.GetColumnDataType(TableName);
dt = dh.DataTypeColumnToDataTable(dt);
}
//导出Excel的时候返回一个文件名,用户选择是否打开
string filePath = eh.ExportExcel(dt, 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;
ExcelHandler eh = new ExcelHandler();
Console.WriteLine(ChoosedExcel);
eh.ImportExcel(ChoosedExcel, "test");
}
}
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;
}
}
}