using HighWayIot.Log4net; using HighWayIot.Repository.service; using HighWayIot.Winform.Business; using Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HighWayIot.Winform.UserControlPages.ParamConfigPages { public partial class ProductionScheduling : UserControl { ZxSchedulingService zxSchedulingService = ZxSchedulingService.Instance; ZxRecipeService zxRecipeService = ZxRecipeService.Instance; List zxRecipeEntities = new List(); List zxSchedulingEntity = new List(); BindingSource bindingSource = new BindingSource(); public ProductionScheduling() { InitializeComponent(); init(); } private void init() { this.SchedulingDataGridView.DataError += new DataGridViewDataErrorEventHandler(SchedulingDataGridView_DataError); zxSchedulingEntity = zxSchedulingService.GetSchedulingInfo(); SchedulingDataGridView.AutoGenerateColumns = false; bindingSource.DataSource = zxSchedulingEntity; SchedulingDataGridView.DataSource = null; SchedulingDataGridView.DataSource = bindingSource; ComboBoxBind(); } /// /// 处理异常数据 /// /// /// /// private void SchedulingDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { return; } /// /// 绑定下拉框数据源 /// private void ComboBoxBind() { zxRecipeEntities = zxRecipeService.GetRecipeInfos(); RecipeName1.DataSource = null; RecipeName2.DataSource = null; List r1 = zxRecipeEntities.Select(x => x.RecipeName).ToList(); r1.Sort(); r1.Insert(0, ""); RecipeName1.DataSource = r1; List r2 = zxRecipeEntities.Select(x => x.RecipeName).ToList(); r2.Sort(); r2.Insert(0, ""); RecipeName2.DataSource = r2; } /// /// 更新设置 /// /// /// private void UpdateConfigButton_Click(object sender, EventArgs e) { if (zxSchedulingService.UpdateSchedulingInfo(zxSchedulingEntity)) { MessageBox.Show("更新成功"); } else { MessageBox.Show("更新失败"); } } /// /// 刷新页面 /// /// /// private void SelectConfigButton_Click(object sender, EventArgs e) { zxSchedulingEntity = zxSchedulingService.GetSchedulingInfo(); bindingSource.DataSource = null; bindingSource.DataSource = zxSchedulingEntity; } /// /// 实现单击一次显示下拉列表框 /// /// /// private void SchedulingDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e) { if (SchedulingDataGridView.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1) { SendKeys.Send("{F4}"); } } /** **第一步: **/ ComboBox cbo = new ComboBox(); private void SchedulingDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (SchedulingDataGridView.CurrentCell.OwningColumn.Name.Contains("RecipeName") && SchedulingDataGridView.CurrentCell.RowIndex != -1) { //保存当前的事件源。为了触发事件后。在取消 cbo = e.Control as ComboBox; //cbo.AutoCompleteSource = AutoCompleteSource.CustomSource; //cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend; cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged); } } /** **第二步: **/ void cbo_SelectedIndexChanged(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; //添加离开combox的事件(重要;) combox.Leave += new EventHandler(combox_Leave); try { //在这里就可以做值是否改变判断 if (combox.SelectedItem != null) { int row = SchedulingDataGridView.CurrentCell.RowIndex; int column = SchedulingDataGridView.CurrentCell.ColumnIndex; string DeviceNo = SchedulingDataGridView.Rows[row].Cells[1].Value.ToString(); if(column == 5) { ZxRecipeEntity recipe = zxRecipeService.GetSingleInfoByRecipeName(combox.Text); if (recipe == null) { MessageBox.Show("存在多个相同的标称尺度或不存在,请检查并维护配方,重启排程界面"); zxSchedulingEntity[row].RecipeCode1 = string.Empty; } else { zxSchedulingEntity[row].RecipeCode1 = recipe.RecipeCode; SqlLogHelper.AddLog($"排程信息操作 机台[{DeviceNo}下模] 配方更改为[{recipe.RecipeCode}]"); } } if (column == 3) { ZxRecipeEntity recipe = zxRecipeService.GetSingleInfoByRecipeName(combox.Text); if (recipe == null) { MessageBox.Show("存在多个相同的标称尺度或不存在,请检查并维护配方,重启排程界面"); zxSchedulingEntity[row].RecipeCode2 = string.Empty; } else { zxSchedulingEntity[row].RecipeCode2 = recipe.RecipeCode; SqlLogHelper.AddLog($"排程信息操作 机台[{DeviceNo}上模] 配方更改为[{recipe.RecipeCode}]"); } } SchedulingDataGridView.Refresh(); } } catch (Exception ex) { LogHelper.Instance.Error("硫化排程发生错误", ex); } } /** **第三步:离开combox时,把事件删除 **/ public void combox_Leave(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; //做完处理,须撤销动态事件 combox.SelectedIndexChanged -= new EventHandler(cbo_SelectedIndexChanged); } } }