目的:
扩展 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; }
运行效果如下图左所示
@H_403_40@
那右边图是什么鬼?
现在还有一个问题没有解决:默认第一次加载出来的数据,并不能完全判断出是否超界限,有时会有一两个能判断,有时完全不能判断,但只需要用鼠标去点击各单元格,它又可以自动识别。暂时没有发现问题原因所在。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的DataGridView带图标的单元格实现代码全部内容,希望文章能够帮你解决DataGridView带图标的单元格实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)