目的: 擴展 C# WinForm 自帶的表格控制項,使其可以自動判斷數據的上下界限值,並標識溢出。 這裡使用的方法是:擴展 表格的列 對象:DataGridViewColumn。 1.創建類:DecimalCheckCell 2.創建類:DecimalCheckColumn 3.現在就可以使用了,在 ...
目的:
擴展 C# WinForm 自帶的表格控制項,使其可以自動判斷數據的上下界限值,並標識溢出。
這裡使用的方法是:擴展 表格的列 對象:DataGridViewColumn。
1.創建類:DecimalCheckCell
/// <summary> /// 可進行範圍檢查的 數值單元格 /// </summary> public class DecimalCheckCell : DataGridViewTextBoxCell { private bool checkMaxValue = false; private bool checkMinValue = false; private decimal maxValue = 0; private decimal minValue = 0; public decimal MaxValue { get { return maxValue; } internal set { maxValue = value; } } public decimal MinValue { get { return minValue; } internal set { minValue = value; } } public bool CheckMaxValue { get { return checkMaxValue; } internal set { checkMaxValue = value; } } public bool CheckMinValue { get { return checkMinValue; } internal set { checkMinValue = value; } } public override object Clone() { DecimalCheckCell c = base.Clone() as DecimalCheckCell; c.checkMaxValue = this.checkMaxValue; c.checkMinValue = this.checkMinValue; c.maxValue = this.maxValue; c.minValue = this.minValue; return c; } protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // Paint the base content base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); // 上下界限溢出判斷 if (this.RowIndex < 0 || this.OwningRow.IsNewRow) // 行序號不為-1,且不是新記錄行(貌似沒用) return; if (value == null) return; decimal vCurValue = Convert.ToDecimal(value); bool overValue = false; Image img = null; if (checkMaxValue) { overValue = vCurValue > maxValue; img = VsTest.Properties.Resources.Undo; // 圖片來自 添加的資源文件 } if (checkMinValue && !overValue) { overValue = vCurValue < minValue; img = VsTest.Properties.Resources.Redo; // 圖片來自 添加的資源文件 } // 將圖片繪製在 數值文本後面 if (overValue && img != null) { var vSize = graphics.MeasureString(vCurValue.ToString(), cellStyle.Font); System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer(); graphics.SetClip(cellBounds); graphics.DrawImageUnscaled(img, new Point(cellBounds.Location.X + (int)vSize.Width, cellBounds.Location.Y)); graphics.EndContainer(container); } } protected override bool SetValue(int rowIndex, object value) { if (rowIndex >= 0) { try { decimal vdeci = Convert.ToDecimal(value); // 篩選非數字 base.ErrorText = string.Empty; } catch (Exception ex) { base.ErrorText = "輸入錯誤" + ex.Message; return false; } } return base.SetValue(rowIndex, value); } }
2.創建類:DecimalCheckColumn
/// <summary> /// 可進行範圍檢查的 數值列 /// </summary> public class DecimalCheckColumn : DataGridViewColumn { private bool checkMaxValue = false; private bool checkMinValue = false; private decimal maxValue = 0; private decimal minValue = 0; public decimal MaxValue { get { return maxValue; } set { maxValue = value; (base.CellTemplate as DecimalCheckCell).MaxValue = value; } } public decimal MinValue { get { return minValue; } set { minValue = value; (base.CellTemplate as DecimalCheckCell).MinValue = value; } } /// <summary> /// 是否對值上界限進行檢查,與MaxValue配合使用 /// </summary> public bool CheckMaxValue { get { return checkMaxValue; } set { checkMaxValue = value; (base.CellTemplate as DecimalCheckCell).CheckMaxValue = value; } } /// <summary> /// 是否對值下界限進行檢查,與MinValue配合使用 /// </summary> public bool CheckMinValue { get { return checkMinValue; } set { checkMinValue = value; (base.CellTemplate as DecimalCheckCell).CheckMinValue = value; } } public DecimalCheckColumn() : base(new DecimalCheckCell()) { } public override object Clone() { DecimalCheckColumn c = base.Clone() as DecimalCheckColumn; c.checkMaxValue = this.checkMaxValue; c.checkMinValue = this.checkMinValue; c.maxValue = this.maxValue; c.minValue = this.minValue; return c; } }
3.現在就可以使用了,在窗體上拖一個 dataGridView 控制項,添加如下代碼:
private void TestForm_Load(object sender, EventArgs e) { InitControlsProperties(); // 初始化 // 綁定數據 DataTable dTabel = new DataTable(); dTabel.Columns.Add("ID",typeof(int)); dTabel.Columns.Add("TestValue",typeof(decimal)); Random rnd = new Random(); for (int i = 0; i < 10; i++) // 隨機10個數 { var vdr = dTabel.NewRow(); vdr[0] = i + 1; vdr[1] = rnd.Next(50); dTabel.Rows.Add(vdr); } this.dataGridView1.DataSource = dTabel; } private void InitControlsProperties() { DecimalCheckColumn ColumnRoleID = new DecimalCheckColumn(); ColumnRoleID.DataPropertyName = "ID"; ColumnRoleID.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; ColumnRoleID.Name = "ID"; ColumnRoleID.HeaderText = "序號"; ColumnRoleID.Width = 50; this.dataGridView1.Columns.Add(ColumnRoleID); DecimalCheckColumn ColumnRoleName = new DecimalCheckColumn(); ColumnRoleName.DataPropertyName = "TestValue"; ColumnRoleName.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; ColumnRoleName.Name = "TestValue"; ColumnRoleName.HeaderText = "測試數據"; ColumnRoleName.Width = 100; ColumnRoleName.CheckMaxValue = true; // 進行最大值檢查 ColumnRoleName.MaxValue = 41; ColumnRoleName.CheckMinValue = true; // 進行最小值檢查 ColumnRoleName.MinValue = 7; this.dataGridView1.Columns.Add(ColumnRoleName); //this.dataGridView1.AllowUserToAddRows = false; //this.dataGridView1.AllowUserToDeleteRows = false; //this.dataGridView1.ReadOnly = true; this.dataGridView1.AutoGenerateColumns = false; }
運行效果如下圖左所示
那右邊圖是什麼鬼?
現在還有一個問題沒有解決:預設第一次載入出來的數據,並不能完全判斷出是否超界限,有時會有一兩個能判斷,有時完全不能判斷,但只需要用滑鼠去點擊各單元格,它又可以自動識別。暫時沒有發現問題原因所在。
[http://www.cnblogs.com/CUIT-DX037/]