174 lines
5.6 KiB
C#
174 lines
5.6 KiB
C#
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
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 数据库访问接口
|
||
|
|
/// </summary>
|
||
|
|
private IDbHelper dbHelper = new OpSqlDataBase();
|
||
|
|
/// <summary>
|
||
|
|
/// 定义DataGridView数据源
|
||
|
|
/// </summary>
|
||
|
|
private BindingList<PLCConfigBase> blPLCConfigBaseList = new BindingList<PLCConfigBase>();
|
||
|
|
public FrmConfigBaseSet()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
}
|
||
|
|
|
||
|
|
//bool IsOrg = true;
|
||
|
|
|
||
|
|
private void FrmConfigBaseSet_Load(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
this.dgvPara.AutoGenerateColumns = false;
|
||
|
|
//IsOrg = false;
|
||
|
|
GetData();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 加载PLC轴参数数据到页面列表
|
||
|
|
/// </summary>
|
||
|
|
private void GetData()
|
||
|
|
{
|
||
|
|
var list = dbHelper.GetPLCConfigBases();
|
||
|
|
blPLCConfigBaseList = new BindingList<PLCConfigBase>(list);
|
||
|
|
dgvPara.DataSource = blPLCConfigBaseList;
|
||
|
|
//dgvPara.Columns[0].Visible = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 导出到Excel
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 保存轴参数信息
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
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());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 关闭窗体
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
this.Close();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 添加记录
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
private void tsmiAdd_Click(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
blPLCConfigBaseList.Add(new PLCConfigBase()
|
||
|
|
{
|
||
|
|
OrderNum = blPLCConfigBaseList.Count + 1,
|
||
|
|
PLCAddress = "D00",
|
||
|
|
PLCRemark = ""
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 删除记录
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="sender"></param>
|
||
|
|
/// <param name="e"></param>
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|