using JY.DAL;
using JY.Inspection.Common;
using JY.Model;
using JY.Utility;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JY.Inspection.Frm
{
public partial class FrmConfigBaseSet : MetroForm
{
///
/// 数据库访问接口
///
private IDbHelper dbHelper = new OpSqlDataBase();
///
/// 定义DataGridView数据源
///
private BindingList blPLCConfigBaseList = new BindingList();
public FrmConfigBaseSet()
{
InitializeComponent();
}
//bool IsOrg = true;
private void FrmConfigBaseSet_Load(object sender, EventArgs e)
{
this.dgvPara.AutoGenerateColumns = false;
//IsOrg = false;
GetData();
}
///
/// 加载PLC轴参数数据到页面列表
///
private void GetData()
{
var list = dbHelper.GetPLCConfigBases();
blPLCConfigBaseList = new BindingList(list);
dgvPara.DataSource = blPLCConfigBaseList;
//dgvPara.Columns[0].Visible = false;
}
///
/// 导出到Excel
///
///
///
private void btnExcelToDgv_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "导入Excel数据库|*.xlsx;"; //打开文件对话框筛选器
if (fd.ShowDialog() == DialogResult.OK)
{
EPPlusExcelHelper excelHepler = new EPPlusExcelHelper(fd.FileName);
DataTable dt = excelHepler.ImportExcel(1);
if (dt != null && dt.Rows.Count > 0)
{
blPLCConfigBaseList.Clear();
foreach (DataRow dr in dt.Rows)
{
blPLCConfigBaseList.Add(new PLCConfigBase()
{
OrderNum = int.Parse(dr["顺序"].ToString()),
PLCAddress = dr["PLC地址"].ToString(),
PLCRemark = dr["地址说明"].ToString()
});
}
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// 保存轴参数信息
///
///
///
private void btnOk_Click(object sender, EventArgs e)
{
try
{
string PlcAddressErr = "";//判断 PLC地址 是否重复
if (blPLCConfigBaseList.Count == 0)
{
MessageBox.Show("列表数据空数据,不能保存!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var duplicateData = blPLCConfigBaseList.GroupBy(p => p.PLCAddress);
foreach (var item in duplicateData)
{
if (item.Count() > 1)
{
PlcAddressErr += $"PLC地址不能重复:{item.FirstOrDefault().PLCAddress}";
}
}
if (PlcAddressErr != "")//重复,需要报错并返出
{
MessageBox.Show(PlcAddressErr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;//返出
}
dbHelper.InsertPLCConfigBase(blPLCConfigBaseList.ToList());
MessageBox.Show("保存成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 关闭窗体
///
///
///
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 添加记录
///
///
///
private void tsmiAdd_Click(object sender, EventArgs e)
{
blPLCConfigBaseList.Add(new PLCConfigBase()
{
OrderNum = blPLCConfigBaseList.Count + 1,
PLCAddress = "D00",
PLCRemark = ""
});
}
///
/// 删除记录
///
///
///
private void tsmiDelete_Click(object sender, EventArgs e)
{
if (dgvPara.RowCount == 0)
{
MessageBox.Show("没有数据需要删除!");
return;
}
if (MessageBox.Show("确定要删除该行?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
int row = dgvPara.CurrentCell.RowIndex;
blPLCConfigBaseList.RemoveAt(row);
}
}
}
}