using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Windows.Forms; using Core.Mes.Client.Comm.Server; using Core.Mes.Client.Comm.Tool; using Core.StlMes.Client.LgClassModel; using CoreFS.CA06; using Infragistics.Win; using Infragistics.Win.UltraWinGrid; namespace Core.StlMes.Client.LgCommon { public partial class ucResAdditiveProcess : UserControl { public ucResAdditiveProcess() { InitializeComponent(); ClsControlPack.RefreshAndAutoSize(ugData); } private OpeBase _ob; private DataTable columns; private string _stove = ""; private string _station = ""; private string _disposalTime = ""; private LG_PROCESS _process; private bool flag = false; private bool _changed = false; public void SetOb(OpeBase ob) { _ob = ob; } public bool Changed { get { return _changed ? _changed : ugData.Rows.Count >= 0 && ugData.Rows.Any(p => (bool)p.Cells["NewRow"].Value); } } public bool CanSave { get { try { ugData.UpdateData(); } catch (Exception) { return false; } return ugData.Rows.Count<=0 || ugData.Rows.All(p => (bool)p.Cells["Valid"].Value); } } public bool SaveDate() { if (_ob == null) return false; if (!Changed || dataTable1.Rows.Count<=0) return true; if (!CanSave) { MessageBox.Show(@"添加料过程数据不正确,无法保存添加料过程数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } var data = dataTable1.AsEnumerable().Select(p => { List> datas = new List>(); for (int i = 3; i < p.ItemArray.Length; i++) { if (p[i] == DBNull.Value || p[i] == null || p[i].ToString().Trim()=="") continue; datas.Add(new Dictionary() { { "Code", dataTable1.Columns[i].ColumnName }, { "Value" , p[i] } }); } return new Dictionary() { { dataTable1.Columns[0].ColumnName, p[0] }, { dataTable1.Columns[1].ColumnName, (p[1]==null|| string.IsNullOrEmpty(p[1].ToString()))?" ":p[1] }, { dataTable1.Columns[2].ColumnName, p[2] }, { "Data",datas } }; } ).ToList(); try { ServerHelper.SetData("Core.LgMes.Server.LgDeviceManager.StlAdditive.SaveAdditivesProcess", new object[] { data,_process.ToString(), _stove, _station,_disposalTime }, _ob); return true; } catch (Exception ex) { return false; } } public void SetData(LG_PROCESS PROCESS,string Stove, string station, string disposalTime) { _stove = Stove; _station = station; _disposalTime = disposalTime; _process = PROCESS; Refresh(); } private void Refresh() { if (_ob == null) return; dataTable1.Rows.Clear(); DataTable datas = ServerHelper.GetData( "Core.LgMes.Server.LgDeviceManager.StlAdditive.GetAdditivesProcess", new object[] { _process.ToString(), _stove, _station, _disposalTime }, _ob); if (datas==null ||datas.Rows.Count <= 0) return; foreach (DataRow row in datas.Rows) { var newRow = dataTable1.NewRow(); newRow["JMDATE"] = row["JMDATE"]; newRow["HOPPERNUMID"] = row["HOPPERNUMID"]; newRow["MATERIELTYPE"] = row["MATERIELTYPE"]; string code = row["MATERIELCODE"].ToString(); string value = row["JMWGT"].ToString(); if(!dataTable1.Columns.Contains(code)) continue; newRow[code] = value; if (CheckRowValid(newRow)) { dataTable1.Rows.Add(newRow); var tempRow = ugData.Rows[ugData.Rows.Count - 1]; tempRow.Cells["Valid"].Value = true; } } if (dataTable1.Rows.Count >= 0) { ClsControlPack.RefreshAndAutoSize(ugData); } _changed = false; } public void RefeshColumns(string strStationCode) { ugData.BeginUpdate(); bool bSuccess; string strMsg; DataSet ds = ClsDataAccessPack.GetData("AddColumns.Select", new ArrayList { strStationCode, strStationCode }, out bSuccess, out strMsg, _ob); if (!bSuccess || ds.Tables.Count <= 0) return; columns = ds.Tables[0]; ValueList vlist = new ValueList(); foreach (DataRow row in columns.Rows) { string TypeCode = row["ITEM_TYPE"].ToString(); string TypeName = row["ITEM_TYPE_NAME"].ToString(); if (vlist.FindByDataValue(TypeCode) == null) { vlist.ValueListItems.Add(TypeCode, TypeName); } } ClsControlPack.SetGridValuelist(ref ugData, "MATERIELTYPE", ref vlist); while (this.dataTable1.Columns.Count > 3) { this.dataTable1.Columns.RemoveAt(3); } try { string strCode = "", strName = ""; for (int i = 0; i < columns.Rows.Count; i++) { strCode = columns.Rows[i]["ITEM_CODE"].ToString(); strName = columns.Rows[i]["ITEM_NAME"].ToString(); if (string.IsNullOrEmpty(strName)) continue; if (!dataTable1.Columns.Contains(strCode)) { DataColumn column = new DataColumn(strCode, typeof(string)) { Caption = strName }; this.dataTable1.Columns.Add(column); ugData.DisplayLayout.Bands[0].Columns[strCode].CellAppearance.TextVAlign = VAlign.Middle; ugData.DisplayLayout.Bands[0].Columns[strCode].CellAppearance.TextHAlign = HAlign.Right; } } for (int i = 3; i < dataTable1.Columns.Count; i++) { List codes = columns.AsEnumerable().Select(p => p["ITEM_CODE"].ToString()).Distinct().ToList(); if (!codes.Contains(dataTable1.Columns[i].ColumnName)) { dataTable1.Columns.RemoveAt(i--); } } } catch { } finally { try { SetAdditiveDataSummary(); ugData.EndUpdate(); _changed = false; } catch { } } } //物料 public void SetAdditiveProcess(string strStationCode,string strDisposalTime) { bool bSuccess; string strMsg; DataSet ds = ClsDataAccessPack.GetData("AddColumns.Select", new ArrayList { strStationCode, strStationCode }, out bSuccess, out strMsg, _ob); if (!bSuccess || ds.Tables.Count<=0) return; ugData.BeginUpdate(); columns = ds.Tables[0]; ValueList vlist = new ValueList(); foreach (DataRow row in columns.Rows) { string TypeCode = row["ITEM_TYPE"].ToString(); string TypeName = row["ITEM_TYPE_NAME"].ToString(); if (vlist.FindByDataValue(TypeCode)==null) { vlist.ValueListItems.Add(TypeCode, TypeName); } } ClsControlPack.SetGridValuelist(ref ugData, "MATERIELTYPE", ref vlist); this.dataTable1.Rows.Clear(); while (this.dataTable1.Columns.Count>3) { this.dataTable1.Columns.RemoveAt(3); } try { string strCode = "", strName = ""; for (int i = 0; i < columns.Rows.Count; i++) { strCode = columns.Rows[i]["ITEM_CODE"].ToString(); strName = columns.Rows[i]["ITEM_NAME"].ToString(); if(string.IsNullOrEmpty(strName)) continue; if (!dataTable1.Columns.Contains(strCode)) { DataColumn column = new DataColumn(strCode, typeof(string)) {Caption = strName}; this.dataTable1.Columns.Add(column); ugData.DisplayLayout.Bands[0].Columns[strCode].CellAppearance.TextVAlign = VAlign.Middle; ugData.DisplayLayout.Bands[0].Columns[strCode].CellAppearance.TextHAlign = HAlign.Right; } } } catch { } finally { try { // UltraGridRow row = ultraGrid1.DisplayLayout.Bands[0].AddNew(); //DataRow drFL= dataTable1.NewRow(); // drFL[0] = "配料方案"; // row.Cells[0].Value = "配料方案"; //this.dataTable1.Rows.Add(drFL); SetAdditiveDataSummary(); ugData.EndUpdate(); _changed = false; } catch { } } } private void SetAdditiveDataSummary() { try { UltraGridBand band = this.ugData.DisplayLayout.Bands[0]; band.Summaries.Clear(); SummarySettings summary = band.Summaries.Add(SummaryType.Count, band.Columns[0]); summary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed; summary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn; summary.Appearance.ForeColor = Color.DarkBlue; summary.Appearance.BackColor = Color.White; summary.Appearance.TextVAlign = VAlign.Middle; summary.Appearance.TextHAlign = HAlign.Center; summary.Appearance.FontData.Bold = DefaultableBoolean.True; summary.DisplayFormat = "投料合计:"; if (columns != null && columns.Rows.Count > 0) { List codes = columns.AsEnumerable().Select(p => p["ITEM_CODE"].ToString()).Distinct().ToList(); for (int idx = 0; idx < codes.Count; idx++) { try { summary = band.Summaries.Add(SummaryType.Sum, band.Columns[codes[idx]]); summary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed; summary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn; summary.Appearance.FontData.Bold = DefaultableBoolean.True; summary.DisplayFormat = " {0:#####.###}"; summary.Appearance.TextVAlign = VAlign.Middle; summary.Appearance.TextHAlign = HAlign.Right; summary.Appearance.ForeColor = Color.DarkBlue; summary.Appearance.BackColor = Color.White; } catch { } } } band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False; } catch { } } private void tsmAdd_Click(object sender, EventArgs e) { if (dataTable1.Columns.Count <= 3) { MessageBox.Show("请先配置添加料"); return; } var dr1 = dataTable1.NewRow(); dr1["JMDATE"] = DateTime.Now; dataTable1.Rows.Add(dr1); var tempRow = ugData.Rows[ugData.Rows.Count - 1]; tempRow.Appearance.BackColor = Color.Orange; tempRow.Cells["Valid"].Value = false; if (dataTable1.Rows.Count <= 1) { ClsControlPack.RefreshAndAutoSize(ugData); } } private void tsmDelete_Click(object sender, EventArgs e) { if (ugData.ActiveRow == null) { MessageBox.Show("请选中一行再进行删除"); return; } if (!(bool)ugData.ActiveRow.Cells["NewRow"].Value) { _changed = true; } dataTable1.Rows.RemoveAt(ugData.ActiveRow.Index); } private void ugTem_KeyPress(object sender, KeyPressEventArgs e) { if (ugData.ActiveRow != null) { try { ugData.UpdateData(); } catch (Exception) { } } } private void ugTem_BeforeRowsDeleted(object sender, Infragistics.Win.UltraWinGrid.BeforeRowsDeletedEventArgs e) { if (e.Rows != null) { StlTempsamplingEntity StlTempsamplingEntity = e.Rows[0].ListObject as StlTempsamplingEntity; if (StlTempsamplingEntity != null && !StlTempsamplingEntity.NewRow) { _changed = true; } } } private void ugTem_ClickCellButton(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e) { e.Cell.Value = DateTime.Now; } private void ugTem_AfterCellUpdate(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e) { var row = ugData.ActiveRow; if (row != null) { if (!CheckRowValid(row) ) { row.Appearance.BackColor = Color.Orange; row.Cells["Valid"].Value = false; } else { row.Appearance.BackColor = Color.LightGreen; row.Cells["Valid"].Value = true; } if (!(bool)row.Cells["NewRow"].Value) { _changed = true; } } //StlTempsamplingEntity StlTempsamplingEntity = ugData.ActiveRow.ListObject as StlTempsamplingEntity; //if (StlTempsamplingEntity != null && StlTempsamplingEntity.NewRow) //{ // if (StlTempsamplingEntity.Startdate == null || StlTempsamplingEntity.Enddate == null || // StlTempsamplingEntity.Startdate > StlTempsamplingEntity.Enddate // || StlTempsamplingEntity.Samplingname == "" // || StlTempsamplingEntity.Samplingvalue == null // ) // { // ugTem.Rows[ugTem.ActiveRow.Index].Appearance.BackColor = Color.Orange; // } // else // { // ugTem.Rows[ugTem.ActiveRow.Index].Appearance.BackColor = Color.LightGreen; // } //} } private bool CheckRowValid(UltraGridRow row) { if (row.Cells["JMDATE"].Value == null || row.Cells["MATERIELTYPE"].Value == null || row.Cells["MATERIELTYPE"].Value == DBNull.Value) return false; /* row.Cells["JMDATE"].Value == null || row.Cells["MATERIELTYPE"].Value == null*/ if (row.Cells.Count <= 5) return false; for (int i = 3; i < row.Cells.Count-2; i++) { double data; if (row.Cells[i].Value != null && double.TryParse(row.Cells[i].Value.ToString(), out data)) { return true; } } return false; } private bool CheckRowValid(DataRow row) { if (row["JMDATE"] == null || row["MATERIELTYPE"] == null) return false; if (row.ItemArray.Length <= 3) return false; for (int i = 3; i < row.ItemArray.Length; i++) { if( row[i]!=null && !string.IsNullOrEmpty(row[i].ToString())) { return true; } } return false; } private void ugTem_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { UIElement uieOver = ugData.DisplayLayout.UIElement.ElementFromPoint(new Point(e.X, e.Y)); if (uieOver != null) { UltraGridRow ugrOver = uieOver.GetContext(typeof(UltraGridRow), true) as UltraGridRow; ugData.ActiveRow = ugrOver; } } } private void ugData_CellChange(object sender, CellEventArgs e) { if (ugData.ActiveRow != null) { try { ugData.UpdateData(); } catch (Exception) { } } if (e.Cell.Column.Key.Equals("MATERIELTYPE", StringComparison.CurrentCultureIgnoreCase)) { for (int i = 3; i < e.Cell.Row.Cells.Count; i++) { var cell = e.Cell.Row.Cells[i]; cell.IgnoreRowColActivation = true; if (cell.Activation == Activation.ActivateOnly) { cell.Value = null; } } } } private void ugData_InitializeRow(object sender, InitializeRowEventArgs e) { string type = e.Row.Cells["MATERIELTYPE"].Value.ToString(); if (type == "") return; List data = columns.AsEnumerable() .Where(p => p["ITEM_TYPE"].ToString() == type) .Select(p => p["ITEM_CODE"].ToString()) .ToList(); for (int i =3; i < e.Row.Cells.Count; i++) { var cell = e.Row.Cells[i]; cell.IgnoreRowColActivation = true; if (data.Contains(cell.Column.Key)) { cell.Activation = Activation.AllowEdit; cell.Appearance.ResetBackColor(); cell.IgnoreRowColActivation = false; } else { cell.Activation = Activation.ActivateOnly; cell.Appearance.BackColor = Color.LightGray; cell.IgnoreRowColActivation = true; } } } } }