using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Data; namespace Mesnac.Controls.Feeding { /// /// 小料列表 /// [ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))] public partial class SmallMaterialList2 : DataGridView { public SmallMaterialList2() { InitializeComponent(); InitMethod(); this.Resize += new EventHandler(SmallMaterialList_Resize); } public SmallMaterialList2(IContainer container) { container.Add(this); InitializeComponent(); InitMethod(); this.Resize += new EventHandler(SmallMaterialList_Resize); } /// /// 初始化方法 /// private void InitMethod() { this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //整行选择模式 this.MultiSelect = false; //禁用多行选项 this.ReadOnly = true; //禁止编辑 this.RowTemplate.Height = 38; this.DefaultCellStyle.Font = new System.Drawing.Font("宋体", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.ColumnHeadersHeight = 42; this.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; this.RowHeadersVisible = false; this.ScrollBars = ScrollBars.None; } protected void SmallMaterialList_Resize(object sender, EventArgs e) { this.SetColumnsStyle(); } private void SetColumnsStyle() { lock (String.Empty) { try { //设定初始列宽 //待调试 if (this.Columns.Contains("小料名称")) { this.Columns["小料名称"].Width = 200; } if (this.Columns.Contains("序号")) { this.Columns["序号"].Width = 50; } if (this.Columns.Contains("小料标重")) { this.Columns["小料标重"].Width = 100; } if (this.Columns.Contains("实重")) { this.Columns["实重"].Width = 100; } if (this.Columns.Contains("误差")) { this.Columns["误差"].Width = 100; } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表控件设置列宽错误:" + ex.Message, ex); } } } #region 自定义属性 private string _dataName; private DataTable _data; private DataTable _cloneData; private bool _bHaveAction; private string _smallMaterialName; private int _smallMaterial; private string _smallMaterialValueName; private double _smallMaterialValue; /// /// 数据源动画属性 /// public string DataName { get { return _dataName; } set { _dataName = value; } } /// /// 正在称量名称 /// public string SmallMaterialName { get { return this._smallMaterialName; } set { this._smallMaterialName = value; } } /// /// 正在称量画面 /// public int SmallMaterial { get { return this._smallMaterial; } set { this._smallMaterial = value; UpdateCurrnetRow(1, value); } } /// /// 小料实际值动画属性 /// public string SmallMaterialValueName { get { return this._smallMaterialValueName; } set { this._smallMaterialValueName = value; } } /// /// 正在称量小料 /// public double SmallMaterialValue { get { return this._smallMaterialValue; } set { this._smallMaterialValue = value; this.UpdateCurrentValue(1, value); } } #endregion #region 数据属性 /// /// 数据源 /// public DataTable Data { get { return _data; } set { _data = value; if (this._cloneData == null || this._cloneData.Rows.Count == 0 || this._cloneData.TableName != this._data.TableName) { InitData(); } } } /// /// 数据初始化 /// private void InitData() { try { if (this._cloneData != null) { this.DataSource = null; this._cloneData.Dispose(); this._cloneData = null; } if (this._data != null) { this._cloneData = this._data.Copy(); this.DataSource = this._cloneData; //禁止排序 for (int i = 0; i < this.ColumnCount; i++) { this.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } this.SetColumnsStyle(); this.ClearSelection(); } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表数据初始化错误:" + ex.Message, ex); } } #endregion /// /// 刷新当前行 /// /// private void UpdateCurrnetRow(int rowIndex,int value) { if (rowIndex < 1) { this.ClearSelection(); } else { if (this._cloneData != null && this._cloneData.Columns.Contains("序号") && this._cloneData.Rows.Count > 0) { if (rowIndex <= this._cloneData.Rows.Count) { DataRow row = this._cloneData.Rows[rowIndex - 1]; object val = row["序号"]; if (val != DBNull.Value) { this.ClearSelection(); if (value == 7 || value == 8) { this.Rows[rowIndex - 1].DefaultCellStyle.BackColor = Color.GreenYellow; } else { this.Rows[rowIndex - 1].DefaultCellStyle.BackColor = Color.White; } } } } } } /// /// 更新实际称量值 /// private void UpdateCurrentValue(int rowIndex, double value) { try { if (rowIndex < 1) { this.ClearSelection(); } else { if (this._cloneData != null) { if (this._cloneData.Columns.Contains("实重")) { if (rowIndex <= this._cloneData.Rows.Count) { DataRow row = this._cloneData.Rows[rowIndex - 1]; if (row != null) { row["实重"] = value; } } } } } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表数据初始化错误:" + ex.Message, ex); } } } }