using Module.Plc.ModbusTcp.Tool; using RS.BLL; using RS.Common; using RS.Model; using RS.Module.Common; using Sunny.UI; 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 MorClient.UserFrmFrame { public partial class FrmScrubbingFormula : Form { /// /// 能否新增 /// bool canAdd = true; /// /// 鼠标所在行数 /// int rowLocate = -1; bool isUpdate = false; int state = 0; DataTable dtResult = new DataTable(); DataGridViewHandle gridHdl = new DataGridViewHandle(); BRecipeWoekManage bRecipeWoekManage = new BRecipeWoekManage(); public FrmScrubbingFormula(int STATE) { InitializeComponent(); CreateNewRecipe(); state = STATE; } /// /// 关闭 /// /// /// private void uiButton3_Click(object sender, EventArgs e) { this.Close(); } #region 按钮动作 /// /// 新增 /// /// /// private void uiButton1_Click(object sender, EventArgs e) { if (canAdd) { canAdd = false; isUpdate = true; ScrubbingRecipeArm scrubbingRecipeArm = new ScrubbingRecipeArm(); scrubbingRecipeArm.scrub_position_one = Common.commonClass_scr1_scrubLeftRight.F_Position_M8; scrubbingRecipeArm.scrub_position_two = Common.commonClass_scr1_scrubLeftRight.F_Position_M8; scrubbingRecipeArm.scrub_speed = 0; scrubbingRecipeArm.move_method = "抬起"; scrubbingRecipeArm.scrub_recipe_name = null; scrubbingRecipeArm.scrub_type = state.ToString(); if (dtResult == null || dtResult.Rows.Count <= 0) dtResult.Rows.Add(scrubbingRecipeArm.scrub_position_one, scrubbingRecipeArm.scrub_position_two, scrubbingRecipeArm.scrub_speed, scrubbingRecipeArm.move_method, scrubbingRecipeArm.scrub_recipe_name, scrubbingRecipeArm.scrub_type); else dtResult.Rows.Add(scrubbingRecipeArm.scrub_recipe_name, scrubbingRecipeArm.scrub_position_one, scrubbingRecipeArm.scrub_position_two, scrubbingRecipeArm.scrub_speed, "抬起"); dataGridView.DataSource = dtResult; gridHdl._dbgrid = dataGridView; gridHdl.GenerateBaseDataGridView(); dataGridView.ReadOnly = false; rowLocate = dataGridView.Rows.Count; } } /// /// 删除 /// /// /// private void uiButton2_Click(object sender, EventArgs e) { if (dataGridView.DataSource != null && dataGridView.Rows.Count > 0) { DialogResult dgRst = MessageBox.Show(this, "您确认删除配方吗?", "警告", MessageBoxButtons.OKCancel); if (dgRst == DialogResult.OK) { // 获取第一个选定单元格 DataGridViewCell selectedCell = dataGridView.SelectedCells[0]; // 获取包含该单元格的行 DataGridViewRow selectedRow = selectedCell.OwningRow; // 获取行索引 int rowIndex = selectedRow.Index; if (rowIndex >= 0 && rowIndex < dataGridView.RowCount) { if (!canAdd && rowLocate == dataGridView.RowCount) { dataGridView.Rows.RemoveAt(rowIndex); rowLocate = 0; } else { DataTable data = bRecipeWoekManage.GetScruRecipeScrInfo(state); string old = data.Rows[rowIndex][0].ToString(); if (!bRecipeWoekManage.whetherDelete("SCR" + state, "scrubing_arm", old)) { //数据库删除 DataGridViewRow dataGridViewRow = dataGridView.Rows[rowIndex] as DataGridViewRow; string recipeName = dataGridViewRow.Cells[0].Value.ToString(); bool result = bRecipeWoekManage.DeletScrubRecipeArmInfo(recipeName); //页面删除 dataGridView.Rows.RemoveAt(rowIndex); if (result) { //MessageBox.Show("删除成功"); GetDataSource(); } else { MessageBox.Show("删除失败"); } } else { MessageBox.Show("当前配方已选择,禁止删除"); } } } else { MessageBox.Show("请选择要删除的行"); } } } else { MessageBox.Show("表中无数据,请先添加数据"); } } /// /// 修改 /// /// /// private void uiButton4_Click(object sender, EventArgs e) { dataGridView.ReadOnly = false; if (dataGridView.DataSource != null && dataGridView.Rows.Count > 0) { DialogResult dgRst = MessageBox.Show(this, "您确认修改配方吗?", "警告", MessageBoxButtons.OKCancel); if (dgRst == DialogResult.OK) { isUpdate = true; } } else { MessageBox.Show("表中无数据,请先添加数据"); } } /// /// 保存 /// /// /// private void uiButton5_Click(object sender, EventArgs e) { if (isUpdate) { // 假设你的DataGridView控件名为dataGridView1 if (dataGridView.SelectedCells.Count > 0) { try { // 获取第一个选定单元格 DataGridViewCell selectedCell = dataGridView.SelectedCells[0]; // 获取包含该单元格的行 DataGridViewRow selectedRow = selectedCell.OwningRow; // 获取行索引 int rowIndex = selectedRow.Index; if (rowIndex >= 0 && rowIndex < dataGridView.RowCount) { List strings = new List(); //判断有没有重复的 for (int i = 0; i < dataGridView.Rows.Count; i++) { string flush_recipe_name = dataGridView.Rows[i].Cells[0].Value.ToString(); if (!string.IsNullOrEmpty(flush_recipe_name)) { if (!strings.Contains(flush_recipe_name)) { strings.Add(flush_recipe_name); } else { MessageBox.Show("配方名重复"); return; } } else { MessageBox.Show("配方名不能为空"); return; } } DataGridViewRow dataGridViewRow = dataGridView.Rows[rowIndex] as DataGridViewRow; ScrubbingRecipeArm scrubbingRecipeArm = new ScrubbingRecipeArm(); scrubbingRecipeArm.move_method = dataGridViewRow.Cells[4].Value.ToString(); scrubbingRecipeArm.scrub_recipe_name = dataGridViewRow.Cells[0].Value.ToString().Replace(" ", ""); scrubbingRecipeArm.scrub_position_one = Convert.ToSingle(dataGridViewRow.Cells[1].Value.ToString()); scrubbingRecipeArm.scrub_position_two = Convert.ToSingle(dataGridViewRow.Cells[2].Value.ToString()); scrubbingRecipeArm.scrub_speed = Convert.ToSingle(dataGridViewRow.Cells[3].Value.ToString()); scrubbingRecipeArm.scrub_type = state.ToString(); if (float.TryParse(dataGridViewRow.Cells[1].Value.ToString(), out float scrub_position_one) && float.TryParse(dataGridViewRow.Cells[2].Value.ToString(), out float scrub_position_two)) { if (!canAdd && rowLocate == dataGridView.RowCount) {//新增 if (!bRecipeWoekManage.whetherScrubbing(scrubbingRecipeArm.scrub_recipe_name, scrubbingRecipeArm.scrub_type))//不重复 { bool result = bRecipeWoekManage.SaveScrubbingRecipeArmInfo(scrubbingRecipeArm); if (result) { canAdd = true; MessageBox.Show("保存成功"); GetDataSource(); } else { MessageBox.Show("保存失败"); } } else { MessageBox.Show("配方名重复"); } } else//这个是更改原有配方 { //不用dtresult是因为不知道什么原因这个数据和控件中数据源一样了 DataTable dataTable = bRecipeWoekManage.GetScruRecipeScrInfo(state); string old_scrub_recipe_name = dataTable.Rows[rowIndex]["scrub_recipe_name"].ToString(); if (!bRecipeWoekManage.whetherDelete("SCR" + state, "scrubing_arm", old_scrub_recipe_name)) { bool result = bRecipeWoekManage.UpdateScrubRecipeArmInfo(scrubbingRecipeArm, old_scrub_recipe_name); if (result) { canAdd = true; MessageBox.Show("保存成功"); GetDataSource(); } else { MessageBox.Show("保存失败"); } } else { string msg = "保存成功"; if (scrubbingRecipeArm.scrub_recipe_name != old_scrub_recipe_name) msg = "名字已还原,数据保存成功"; scrubbingRecipeArm.scrub_recipe_name = old_scrub_recipe_name; bool result = bRecipeWoekManage.UpdateScrubRecipeArmInfo(scrubbingRecipeArm, old_scrub_recipe_name); if (result) { canAdd = true; MessageBox.Show(msg); GetDataSource(); } else { MessageBox.Show("保存失败"); } } } } else { MessageBox.Show("位置1或位置2数据无效"); } } else { MessageBox.Show("未选中数据行"); } } catch (Exception ex) { MessageBox.Show("出现错误,请联系管理员"); } } else { // 如果没有选定任何单元格,则显示消息 MessageBox.Show("没有选定任何单元格。"); } } else { MessageBox.Show("请先点击修改"); } dataGridView.ReadOnly = true; } #endregion 按钮动作 #region DataGridView /// /// 加载数据 /// /// 配方名 public void CreateNewRecipe() { #region DataGridViewTextBoxColumn textBoxColumn1 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn textBoxColumn2 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn textBoxColumn3 = new DataGridViewTextBoxColumn(); DataGridViewTextBoxColumn textBoxColumn4 = new DataGridViewTextBoxColumn(); DataGridViewComboBoxColumn comboBoxColumn5 = new DataGridViewComboBoxColumn(); // 如果需要绑定到数据源的属性,请设置此属性 textBoxColumn1.DataPropertyName = "scrub_recipe_name"; textBoxColumn2.DataPropertyName = "scrub_position_one"; textBoxColumn3.DataPropertyName = "scrub_position_two"; textBoxColumn4.DataPropertyName = "scrub_speed"; // 显示为带有一位位小数的浮点数 textBoxColumn2.DefaultCellStyle.Format = "N2"; textBoxColumn3.DefaultCellStyle.Format = "N2"; textBoxColumn4.DefaultCellStyle.Format = "N2"; // 如果需要绑定到数据源的属性,请设置此属性 comboBoxColumn5.DataPropertyName = "move_method"; List vs = new List(); vs.Add("抬起"); vs.Add("下压"); comboBoxColumn5.DataSource = vs; // 将列添加到 DataGridView 的 Columns 集合中 dataGridView.Columns.Add(textBoxColumn1); dataGridView.Columns.Add(textBoxColumn2); dataGridView.Columns.Add(textBoxColumn3); dataGridView.Columns.Add(textBoxColumn4); dataGridView.Columns.Add(comboBoxColumn5); //设置显示列名称 dataGridView.Columns[0].HeaderText = "配方名称"; dataGridView.Columns[1].HeaderText = "位置1"; dataGridView.Columns[2].HeaderText = "位置2"; dataGridView.Columns[3].HeaderText = "速度"; dataGridView.Columns[4].HeaderText = "移动方式"; #endregion gridHdl._dbgrid = dataGridView; gridHdl.GenerateBaseDataGridView(); foreach (DataGridViewColumn column in dataGridView.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } } /// /// 数据源 根据配方名 查询所有步骤 /// /// public void GetDataSource() { dtResult = bRecipeWoekManage.GetScruRecipeScrInfo(state); if (dtResult == null || dtResult.Rows.Count <= 0) { dtResult = new DataTable(); dtResult.Columns.Add("scrub_position_one", typeof(float)); dtResult.Columns.Add("scrub_position_two", typeof(float)); dtResult.Columns.Add("scrub_speed", typeof(float)); dtResult.Columns.Add("move_method", typeof(string)); dtResult.Columns.Add("scrub_recipe_name", typeof(string)); dtResult.Columns.Add("scrub_type", typeof(string)); } dataGridView.DataSource = dtResult; } private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { } #endregion DataGridView private void FrmScrubbingFormula_Load(object sender, EventArgs e) { if (PublicParams.currUserInfo.userGroup == "Operator") { this.Enabled = false; } else { this.Enabled = true; } } } }