DataGridView带图标的单元格实现
生活随笔
收集整理的這篇文章主要介紹了
DataGridView带图标的单元格实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目的:
擴展 C# WinForm 自帶的表格控件,使其可以自動判斷數據的上下界限值,并標識溢出。
這里使用的方法是:擴展 表格的列 對象:DataGridViewColumn。
1.創建類:DataGridViewDecimalCheckCell.cs
public class DataGridViewDecimalCheckCell : 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(){DataGridViewDecimalCheckCell c = base.Clone() as DataGridViewDecimalCheckCell;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 contentbase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,value, formattedValue, errorText, cellStyle,advancedBorderStyle, paintParts);// 上下界限溢出判斷if (rowIndex < 0 || this.OwningRow.IsNewRow) // 行序號不為-1,且不是新記錄行return;if (value == null) return;if (value.GetType() == typeof(DBNull)) 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.創建類:DataGridViewDecimalCheckColumn.cs
public class DataGridViewDecimalCheckColumn : 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 DataGridViewDecimalCheckCell).MaxValue = value;}}public decimal MinValue{get { return minValue; }set{minValue = value;(base.CellTemplate as DataGridViewDecimalCheckCell).MinValue = value;}}/// <summary>/// 是否對值上界限進行檢查,與MaxValue配合使用/// </summary>public bool CheckMaxValue{get { return checkMaxValue; }set{checkMaxValue = value;(base.CellTemplate as DataGridViewDecimalCheckCell).CheckMaxValue = value;}}/// <summary>/// 是否對值下界限進行檢查,與MinValue配合使用/// </summary>public bool CheckMinValue{get { return checkMinValue; }set{checkMinValue = value;(base.CellTemplate as DataGridViewDecimalCheckCell).CheckMinValue = value;}}public DataGridViewDecimalCheckColumn(): base(new DataGridViewDecimalCheckCell()){}public override object Clone(){DataGridViewDecimalCheckColumn c = base.Clone() as DataGridViewDecimalCheckColumn;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(){var vColumnID = new DataGridViewDecimalCheckColumn();vColumnID.DataPropertyName = "ID";vColumnID.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;vColumnID.Name = "ID";vColumnID.HeaderText = "序號";vColumnID.Width = 50;this.dataGridView1.Columns.Add(vColumnID);var vColumnValue = new DataGridViewDecimalCheckColumn();vColumnValue.DataPropertyName = "TestValue";vColumnValue.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;vColumnValue.Name = "TestValue";vColumnValue.HeaderText = "測試數據";vColumnValue.Width = 100;vColumnValue.CheckMaxValue = true; // 進行最大值檢查vColumnValue.MaxValue = 41;vColumnValue.CheckMinValue = true; // 進行最小值檢查vColumnValue.MinValue = 7;this.dataGridView1.Columns.Add(vColumnValue);//this.dataGridView1.AllowUserToAddRows = false;//this.dataGridView1.AllowUserToDeleteRows = false;//this.dataGridView1.ReadOnly = true;this.dataGridView1.AutoGenerateColumns = false;}效果圖:
?
?[http://www.cnblogs.com/CUIT-DX037/]
?
轉載于:https://www.cnblogs.com/CUIT-DX037/p/7354057.html
總結
以上是生活随笔為你收集整理的DataGridView带图标的单元格实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10安装cmder和WSL
- 下一篇: JavaScript — 原生js实现上