using JY.DAL; using JY.Model; 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 FrmParaConfig : MetroFramework.Forms.MetroForm { public delegate void SendParamIN(); public SendParamIN sendParamIN; /// /// 数据库访问接口 /// private IDbHelper dbHelper = new OpSqlDataBase(); /// /// 定义DataGridView数据源 /// private BindingList blPLCConfigBaseList = new BindingList(); public FrmParaConfig() { InitializeComponent(); } private void FrmParaEdit_Load(object sender, EventArgs e) { this.dgvEdit.AutoGenerateColumns = false; this.dgvEdit.DataSource = blPLCConfigBaseList; setComb(); } /// /// 加载产品型号下拉 /// private void setComb() { var list = dbHelper.GetProductModelList(); cmbProductModel.DataSource = list; cmbProductModel.DisplayMember = "ModelName"; cmbProductModel.ValueMember = "ModelName"; } /// /// 保存数据 /// /// /// private void btnOK_Click(object sender, EventArgs e) { string strModelType = cmbProductModel.Text.Trim(); if (string.IsNullOrEmpty(strModelType) || blPLCConfigBaseList.Count <= 0) { MessageBox.Show("产品型号和参数列表不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { dbHelper.AddProductParaList(blPLCConfigBaseList.ToList()); MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("保存失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// 添加型号 /// /// /// private void btnAddProductModel_Click(object sender, EventArgs e) { var productModel = new ProductModel(); productModel.ModelName = txtProductModel.Text; productModel.Remark = txtProductModel.Text; if (string.IsNullOrEmpty(productModel.ModelName)) { MessageBox.Show("产品型号不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { dbHelper.AddProductModel(productModel); setComb(); MessageBox.Show("新增成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("新增失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } //同步刷新主界面的类型选项 sendParamIN(); } /// /// 删除型号 /// /// /// private void btnDelProductModel_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(cmbProductModel.Text.Trim())) { MessageBox.Show("产品型号不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (MessageBox.Show("删除该型号设置将无法恢复,确定要删除该产品型号?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { try { dbHelper.DelProductModel(cmbProductModel.Text); setComb(); MessageBox.Show("删除成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("删除失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// /// 产品型号下拉框选择变更事件 /// /// /// private void cmbProductModel_SelectedIndexChanged(object sender, EventArgs e) { var list = dbHelper.GetProductParaByProdModel(cmbProductModel.Text); blPLCConfigBaseList.Clear(); foreach (var item in list) { blPLCConfigBaseList.Add(item); } } /// /// 关闭事件 /// /// /// private void FrmParaEdit_FormClosing(object sender, FormClosingEventArgs e) { this.DialogResult = DialogResult.OK; } /// /// DataGrid自动添加行号 /// /// /// private void dgvEdit_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //自动编号,与数据无关 Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvEdit.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvEdit.RowHeadersDefaultCellStyle.Font, rectangle, dgvEdit.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); } } }