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; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Technical; using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe; namespace Mesnac.Action.ChemicalWeighing.Product.PptPlan { public partial class FrmProPlan : Form { #region 字段定义 //DateTimePicker dtp = new DateTimePicker(); //这里实例化一个DateTimePicker控件,datagridview控件不单元格不能设置时间设置控件。 //Rectangle _Rectangle; //鼠标单击DGV时,获取大小及位置。 String currentRecipeName; //当前选择的配方名称 List _allPmtRecipe = null; //所有配方数据 int planNum = 0; String batchNum; #endregion #region 构造方法 public FrmProPlan() { InitializeComponent(); //dataGridView1.Controls.Add(dtp); //把时间控件加入DataGridView //dtp.Visible = false; //先不让它显示 //dtp.Format = DateTimePickerFormat.Custom; //设置日期格式为2010-08-05 //dtp.CustomFormat = "yyyy-MM-dd HH:mm:ss"; //dtp.TextChanged += new EventHandler(dtp_TextChange); //为时间控件加入事件dtp_TextChange } #endregion #region 方法定义 /// /// 当日计划显示 /// public void InitDataGridViewData() { //获取当日计划 List lst = null; lst = TechnicalHelper.GetRT_PlanList(); if (lst != null && lst.Count > 0) { foreach (RT_Plan replan in lst) { DataGridViewRow dgvr = new DataGridViewRow(); foreach (DataGridViewColumn c in this.dataGridView1.Columns) { dgvr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell); } dgvr.Cells[0].Value = replan.Recipe_Name; dgvr.Cells[1].Value = replan.Mixer_Line; dgvr.Cells[2].Value = replan.Version; dgvr.Cells[3].Value = replan.Plan_Id; dgvr.Cells[4].Value = replan.Plan_Num; dgvr.Cells[5].Value = replan.Real_Num; dgvr.Cells[6].Value = replan.Weight_Man; dgvr.Cells[7].Value = replan.Plan_Batch; dgvr.Cells[8].Value = replan.Plan_State; dgvr.Cells[9].Value = replan.Start_Date; dgvr.Cells[10].Value = replan.Duration_Time; dgvr.Cells[11].Value = replan.End_Date; this.dataGridView1.Rows.Add(dgvr); } } } /// /// DGV中下拉框数据获取 /// /// 行号 public void InitComboxData(int rowNum) { //获取所有配方信息 List lst = null; lst = TechnicalHelper.GetPmt_recipeList(); if (lst != null && lst.Count > 0) { _allPmtRecipe = lst; foreach (Pmt_recipe pmtRecipe in lst) { (this.dataGridView1.Rows[rowNum].Cells[0] as DataGridViewComboBoxCell).Items.Add(pmtRecipe.Recipe_Name); } } } /// /// 获取当前班次 /// public String GetCurrentShiftime() { String reStr = null; List lst = TechnicalHelper.GetPmt_ShiftimeList(); if(lst != null && lst.Count >0) { foreach(Pmt_Shiftime pmtShiftime in lst) { DateTime startTime = Convert.ToDateTime(pmtShiftime.Shift_st); DateTime endTime = Convert.ToDateTime(pmtShiftime.Shift_et); if (DateTime.Now > startTime && DateTime.Now < endTime) { reStr = pmtShiftime.Shift_name; } } } return reStr; } #endregion #region 事件处理 private void FrmProPlan_Load(object sender, EventArgs e) { this.InitComboxData(0); this.InitDataGridViewData(); } //private void dtp_TextChange(object sender, EventArgs e) //{ // dataGridView1.CurrentCell.Value = dtp.Text; //时间控件选择时间时,就把时间赋给所在的单元格 //} //private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) //{ // if (e.ColumnIndex == 9 || e.ColumnIndex == 11) // { // _Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小 // dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件 // dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件 // dtp.Visible = true; //可以显示控件了 // } // else // { // dtp.Visible = false; // } //} //private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) //{ // dtp.Visible = false; //} //private void dataGridView1_Scroll(object sender, ScrollEventArgs e) //{ // dtp.Visible = false; //} private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { int currentRowNum = this.dataGridView1.NewRowIndex; InitComboxData(currentRowNum); planNum++; } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dataGridView1.CurrentCell != null && e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { //创建下拉列表框改变事件 (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged); //创建下拉列表框离开焦点事件 添加该事件是为了删除动态事件 以免多次调用 (e.Control as ComboBox).Leave += new EventHandler(ComboBox_Leave); } } //离开焦点事件 private void ComboBox_Leave(object sender, EventArgs e) { //删除动态调用 (sender as ComboBox).SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged); (sender as ComboBox).Leave -= new EventHandler(ComboBox_Leave); } //下拉列表框改变事件 private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) { //选择后自动填充右侧内容 //(1)先获取当前选择的行号 int currentRowNum = this.dataGridView1.CurrentRow.Index; //(2)获取当前选中的配方名称 currentRecipeName = dataGridView1.CurrentCell.EditedFormattedValue.ToString(); //(3)根据配方名称获取配方所有信息并填充 if (_allPmtRecipe.Exists(tm => tm.Recipe_Name == currentRecipeName)) { Pmt_recipe pmtRecipe = _allPmtRecipe.FindLast(tm => tm.Recipe_Name == currentRecipeName); if (pmtRecipe != null) { this.dataGridView1.Rows[currentRowNum].Cells[1].Value = pmtRecipe.Mixer_line; this.dataGridView1.Rows[currentRowNum].Cells[2].Value = pmtRecipe.Version; //(4)自动生成批号 batchNum = (DateTime.Now.Year).ToString() + (DateTime.Now.Month).ToString() + (DateTime.Now.Day).ToString() + "X" + planNum.ToString().PadLeft(4, '0'); this.dataGridView1.Rows[currentRowNum].Cells[3].Value = batchNum; //(5)班次信息自动填充 this.dataGridView1.Rows[currentRowNum].Cells[7].Value = GetCurrentShiftime(); this.dataGridView1.Rows[currentRowNum].Cells[9].Value = DateTime.Now.ToString(); } } } #endregion } }