using JinYuan.Helper; using JinYuan.Models; using JinYuan.VirtualDataLibrary; using Language; using SqlSugar; 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 LargeSquareOne { public partial class FrmRecipe : MultiLanguageForm { public FrmRecipe() { InitializeComponent(); ////设置控件样式,去除缓冲 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } #region 减少闪烁 protected override CreateParams CreateParams { get { CreateParams parss = base.CreateParams; parss.ExStyle |= 0x02000000; return parss; } } #endregion Dictionary disOldValue = new System.Collections.Generic.Dictionary(); /// /// 定义DataGridView数据源 /// private BindingList blPLCConfigBaseList = new BindingList(); private void FrmRecipe_Load(object sender, EventArgs e) { this.dgvPara.AutoGenerateColumns = false; this.dgvPara.DataSource = blPLCConfigBaseList; GetProdModel(); GetProductParam(0); } private void GetProdModel() { try { var productList = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetProdTypeSql()); dgvProdModel.DataSource = productList; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #region 选择产品型号 /// /// 点击单元格 /// /// /// private void dgvProdModel_CellClick(object sender, DataGridViewCellEventArgs e) { int row = dgvProdModel.CurrentCell.RowIndex; GetProductParam(row); } public void GetProductParam(int row) { try { if (dgvProdModel.Rows.Count > 0) { string strModelType = dgvProdModel.Rows[row].Cells["ModelName"].Value.ToString(); lb_Model.Text = strModelType; var list = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetProductParaByProdModel(strModelType)); disOldValue.Clear(); for (int i = 0; i < list.Count; i++) { if (!disOldValue.ContainsKey(list[i].Remark)) { disOldValue.Add(list[i].Remark, $"{list[i].ParaValueMin},{list[i].ParaValueMax}"); } } dgvPara.DataSource = list; } } catch (Exception ex) { MessageBox.Show("加载数据失败" + ex.Message); } } #endregion /// /// 添加数据 /// /// /// private void btn_Add_Click(object sender, EventArgs e) { var productModel = new ProductModel(); productModel.ModelName = txtProductModel.Text; productModel.MaterialCode = txtMaterialCode.Text; productModel.Remark = txtProductModel.Text; if (string.IsNullOrEmpty(productModel.ModelName)) { new FrmMsgBoxOutWithAck(2, "产品型号不能为空!", "添加型号").Show(); return; } if (string.IsNullOrEmpty(productModel.MaterialCode)) { new FrmMsgBoxOutWithAck(2, "型号对应的物料编码不能为空!", "添加型号").Show(); return; } try { bool b = CommonMethods.db.AddReturnBool(productModel); if (b) { GetProdModel(); CommonMethods.ChangeParamData(); new FrmMsgBoxOutWithAck(1, "添加成功!", "添加型号").Show(); } else { new FrmMsgBoxOutWithAck(3, "添加失败!", "添加型号").Show(); } } catch (Exception ex) { if (ex.Message.Contains("重复键")) { LogHelper.Instance.WriteEX(ex); new FrmMsgBoxOutWithAck(3, "添加失败:已存在改型号,重复新增!", "添加型号").Show(); } else { LogHelper.Instance.WriteEX(ex); new FrmMsgBoxOutWithAck(3, $"添加失败:{ex.Message}", "添加型号").Show(); } } //同步刷新主界面的类型选项 //sendParamIN(); } private void btn_Save_Click(object sender, EventArgs e) { if (dgvProdModel.Rows.Count > 0 && dgvProdModel.CurrentCell.RowIndex > -1) { int row = dgvProdModel.CurrentCell.RowIndex; string strModelType = dgvProdModel.Rows[row].Cells["ModelName"].Value.ToString(); if (string.IsNullOrEmpty(strModelType) || dgvPara.Rows.Count <= 0) { new FrmMsgBoxOutWithAck(2, "产品型号和参数列表不能为空!", "保存参数").Show(); return; } try { List list = (List)dgvPara.DataSource; int f = CommonMethods.db.Delete(t => t.ModelName == strModelType); if (f >= 0) { bool b = CommonMethods.db.AddReturnBool(list); if (b) { GetProdModel(); CommonMethods.ChangeParamData(); new FrmMsgBoxOutWithAck(1, "保存成功!", "保存参数").Show(); } else { new FrmMsgBoxOutWithAck(3, "保存失败!", "保存参数").Show(); } } } catch (Exception ex) { new FrmMsgBoxOutWithAck(3, "保存失败", "保存参数").Show(); } } else { new FrmMsgBoxOutWithAck(1, "请选择产品型号", "保存参数").Show(); } } private void btnDelProductModel_Click(object sender, EventArgs e) { if (dgvProdModel.Rows.Count > 0 && dgvProdModel.CurrentCell.RowIndex > -1) { int row = dgvProdModel.CurrentCell.RowIndex; string strModelType = dgvProdModel.Rows[row].Cells["ModelName"].Value.ToString(); if (string.IsNullOrEmpty(strModelType)) { new FrmMsgBoxOutWithAck(2, "选择要删除的生产型号", "删除型号").Show(); return; } DialogResult dialogResult = new FrmMsgBoxWithAck($"是否要删除型号【{strModelType}】", "删除型号").ShowDialog(); if (dialogResult == DialogResult.OK) { try { int f = CommonMethods.db.Delete(t => t.ModelName == strModelType); if (f > 0) { GetProdModel(); CommonMethods.ChangeParamData(); } new FrmMsgBoxOutWithAck(1, "删除成功!", "删除型号").Show(); } catch (Exception ex) { new FrmMsgBoxOutWithAck(1, $"删除失败:{ex.Message}", "删除型号").Show(); } } } } private void dgvProdModel_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvProdModel, e); } private void dgvPara_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvPara, e); } } }