using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Mesnac.Controls.Feeding { /// /// 小料列表 /// [ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))] public partial class SmallMaterialList : DataGridView { public SmallMaterialList() { InitializeComponent(); InitMethod(); this.Resize += new EventHandler(SmallMaterialList_Resize); } public SmallMaterialList(IContainer container) { container.Add(this); InitializeComponent(); InitMethod(); this.Resize += new EventHandler(SmallMaterialList_Resize); } protected void SmallMaterialList_Resize(object sender, EventArgs e) { this.SetColumnsStyle(); } /// /// 初始化方法 /// 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; } #region 自定义属性 private string _dataName; private DataTable _data; private DataTable _cloneData; private bool _bHaveAction; private string _smallMaterialName1; private int _smallMaterial1; private string _smallMaterialName2; private int _smallMaterial2; private string _smallMaterialName3; private int _smallMaterial3; private string _smallMaterialName4; private int _smallMaterial4; private string _smallMaterialName5; private int _smallMaterial5; private string _smallMaterialName6; private int _smallMaterial6; private string _smallMaterialValueName1; private double _smallMaterialValue1; private string _smallMaterialValueName2; private double _smallMaterialValue2; private string _smallMaterialValueName3; private double _smallMaterialValue3; private string _smallMaterialValueName4; private double _smallMaterialValue4; private string _smallMaterialValueName5; private double _smallMaterialValue5; private string _smallMaterialValueName6; private double _smallMaterialValue6; #region 动画属性 /// /// 是否有动画 /// public bool bHaveAction { get { return _bHaveAction; } set { _bHaveAction = value; } } /// /// 数据源动画属性 /// public string DataName { get { return _dataName; } set { _dataName = value; } } /// /// 正在称量第1种小料动画属性 /// public string SmallMaterialName1 { get { return this._smallMaterialName1; } set { this._smallMaterialName1 = value; } } /// /// 正在称量第2种小料动画属性 /// public string SmallMaterialName2 { get { return this._smallMaterialName2; } set { this._smallMaterialName2 = value; } } /// /// 正在称量第3种小料动画属性 /// public string SmallMaterialName3 { get { return this._smallMaterialName3; } set { this._smallMaterialName3 = value; } } /// /// 正在称量第4种小料动画属性 /// public string SmallMaterialName4 { get { return this._smallMaterialName4; } set { this._smallMaterialName4 = value; } } /// /// 正在称量第5种小料动画属性 /// public string SmallMaterialName5 { get { return this._smallMaterialName5; } set { this._smallMaterialName5 = value; } }/// /// 正在称量第6种小料动画属性 /// public string SmallMaterialName6 { get { return this._smallMaterialName6; } set { this._smallMaterialName6 = value; } } /// /// 第1种小料实际值动画属性 /// public string SmallMaterialValueName1 { get { return this._smallMaterialValueName1; } set { this._smallMaterialValueName1 = value; } } /// /// 第2种小料实际值动画属性 /// public string SmallMaterialValueName2 { get { return this._smallMaterialValueName2; } set { this._smallMaterialValueName2 = value; } } /// /// 第3种小料实际值动画属性 /// public string SmallMaterialValueName3 { get { return this._smallMaterialValueName3; } set { this._smallMaterialValueName3 = value; } } /// /// 第4种小料实际值动画属性 /// public string SmallMaterialValueName4 { get { return this._smallMaterialValueName4; } set { this._smallMaterialValueName4 = value; } } /// /// 第5种小料实际值动画属性 /// public string SmallMaterialValueName5 { get { return this._smallMaterialValueName5; } set { this._smallMaterialValueName5 = value; } } /// /// 第6种小料实际值动画属性 /// public string SmallMaterialValueName6 { get { return this._smallMaterialValueName6; } set { this._smallMaterialValueName6 = value; } } #endregion #region 数据属性 /// /// 数据源 /// public DataTable Data { get { return _data; } set { _data = value; if (this._cloneData == null || this._cloneData.TableName != this._data.TableName) { InitData(); } } } /// /// 数据初始化 /// private void InitData() { 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(); //设置初始选中行 //if (this.Rows.Count > 1) //{ // this.Rows[0].Selected = true; // this.CurrentCell = this.Rows[0].Cells[0]; //} //else if (this.Rows.Count == 1) //{ // this.Rows[0].Selected = false; //} //else //{ // DataRow row = this._cloneData.NewRow(); // this.Rows.Add(row); // this.Rows[0].Selected = false; //} } } private void SetColumnsStyle() { try { //设定初始列宽 //待调试 //if (this.Columns.Contains("小料名称")) //{ // this.Columns["小料名称"].Width = 400; //} if (this.Columns.Contains("序号")) { this.Columns["序号"].Width = 50; } if (this.Columns.Contains("小料标重")) { this.Columns["小料标重"].Width = 90; } if (this.Columns.Contains("实重")) { this.Columns["实重"].Width = 90; } if (this.Columns.Contains("误差")) { this.Columns["误差"].Width = 100; } } catch (Exception ex) { } } /// /// 正在称量第1种小料 /// public int SmallMaterial1 { get { return this._smallMaterial1; } set { this._smallMaterial1 = value; this.RefreshCurrentRow(); } } /// /// 正在称量第2种小料 /// public int SmallMaterial2 { get { return this._smallMaterial2; } set { this._smallMaterial2 = value; this.RefreshCurrentRow(); } } /// /// 正在称量第3种小料 /// public int SmallMaterial3 { get { return this._smallMaterial3; } set { this._smallMaterial3 = value; this.RefreshCurrentRow(); } } /// /// 正在称量第4种小料 /// public int SmallMaterial4 { get { return this._smallMaterial4; } set { this._smallMaterial4 = value; this.RefreshCurrentRow(); } } /// /// 正在称量第5种小料 /// public int SmallMaterial5 { get { return this._smallMaterial5; } set { this._smallMaterial5 = value; this.RefreshCurrentRow(); } } /// /// 正在称量第6种小料 /// public int SmallMaterial6 { get { return this._smallMaterial6; } set { this._smallMaterial6 = value; this.RefreshCurrentRow(); } } /// /// 第1种小料实际值 /// public double SmallMaterialValue1 { get { return this._smallMaterialValue1; } set { this._smallMaterialValue1 = value; this.RefreshCurrentValue(1, value); } } /// /// 第2种小料实际值 /// public double SmallMaterialValue2 { get { return this._smallMaterialValue2; } set { this._smallMaterialValue2 = value; this.RefreshCurrentValue(2, value); } } /// /// 第3种小料实际值 /// public double SmallMaterialValue3 { get { return this._smallMaterialValue3; } set { this._smallMaterialValue3 = value; this.RefreshCurrentValue(3, value); } } /// /// 第4种小料实际值 /// public double SmallMaterialValue4 { get { return this._smallMaterialValue4; } set { this._smallMaterialValue4 = value; this.RefreshCurrentValue(4, value); } } /// /// 第5种小料实际值 /// public double SmallMaterialValue5 { get { return this._smallMaterialValue5; } set { this._smallMaterialValue5 = value; this.RefreshCurrentValue(5, value); } } /// /// 第6种小料实际值 /// public double SmallMaterialValue6 { get { return this._smallMaterialValue6; } set { this._smallMaterialValue6 = value; this.RefreshCurrentValue(6, value); } } #endregion /// /// 刷新选中行 /// private void RefreshCurrentRow() { if (this._smallMaterial6 == 1) { this.UpdateSelectRow(6); } else if (this._smallMaterial5 == 1) { this.UpdateSelectRow(5); } else if (this._smallMaterial4 == 1) { this.UpdateSelectRow(4); } else if (this._smallMaterial3 == 1) { this.UpdateSelectRow(3); } else if (this._smallMaterial2 == 1) { this.UpdateSelectRow(2); } else if (this._smallMaterial1 == 1) { this.UpdateSelectRow(1); } else { this.ClearSelection(); } } /// /// 刷新当前行 /// /// private void UpdateSelectRow(int rowIndex) { 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(); this.Rows[rowIndex - 1].Selected = true; this.CurrentCell = this.Rows[rowIndex - 1].Cells[0]; } } } } } /// /// 刷新当前值 /// /// private void RefreshCurrentValue(int index, double value) { if (this._smallMaterial6 == 1 && index == 6) { this.UpdateCurrentValue(6, value); } else if (this._smallMaterial5 == 1 && index == 5) { this.UpdateCurrentValue(5, value); } else if (this._smallMaterial4 == 1 && index == 4) { this.UpdateCurrentValue(4, value); } else if (this._smallMaterial3 == 1 && index == 3) { this.UpdateCurrentValue(3, value); } else if (this._smallMaterial2 == 1 && index == 2) { this.UpdateCurrentValue(2, value); } else if (this._smallMaterial1 == 1 && index == 1) { this.UpdateCurrentValue(1, value); } } /// /// 更新实际称量值 /// 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) { } } #endregion } }