目的:

扩展 C# WinForm 自带的表格控件,使其可以自动判断数据的上下界限值,并标识溢出。

这里使用的方法是:扩展 表格的列 对象:DataGridViewColumn。

1.创建类:DataGridViewDecimalCheckCell.cs

    public class DataGridViewDecimalCheckCell : DataGridViewTextBoxCell
{
private bool checkMaxValue = false;
private bool checkMinValue = false;
private decimal maxValue = ;
private decimal minValue = ; 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 content
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
value, formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts); // 上下界限溢出判断
if (rowIndex < || 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 >= )
{
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 = ;
private decimal minValue = ; 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 = ; i < ; i++) // 随机10个数
{
var vdr = dTabel.NewRow();
vdr[] = i + ;
vdr[] = rnd.Next();
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 = ;
this.dataGridView1.Columns.Add(vColumnID); var vColumnValue = new DataGridViewDecimalCheckColumn();
vColumnValue.DataPropertyName = "TestValue";
vColumnValue.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
vColumnValue.Name = "TestValue";
vColumnValue.HeaderText = "测试数据";
vColumnValue.Width = ; vColumnValue.CheckMaxValue = true; // 进行最大值检查
vColumnValue.MaxValue = ;
vColumnValue.CheckMinValue = true; // 进行最小值检查
vColumnValue.MinValue = ; 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/]

DataGridView带图标的单元格实现的更多相关文章

  1. C# DataGridView中指定的单元格不能编辑

    注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. ReadOnly属性的使用 DataGridView内所有的单元格不能编辑 当DataGridView.Re ...

  2. c#DataGridView数据绑定示例——格式化单元格的内容(转)

    转自http://blog.csdn.net/testcs_dn/article/details/37834063 c#DataGridView数据绑定示例 格式化单元格的内容 在使用DataGrid ...

  3. Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)

    项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...

  4. datagridview 纵向 横向 合并单元格

    datagridview 单元格合并:纵向以及横向合并参考了csdn上不知哪位的代码,具体哪位找不到连接了. 纵向合并: /// <summary> /// 纵向合并,即合并数据项的值 / ...

  5. DataGridView根据条件给单元格绑定图片

    代码区: private void Form1_Load(object sender, EventArgs e) { myClass.mySqliteAPI conn = new myClass.my ...

  6. DataGridView单元格内容自动匹配下拉显示

    页面显示数据使用的控件是ComponentFactory.Krypton.Toolkit中的KryptonDataGridView控件.在指定“商品”单元格中需要根据用户输入内容自动匹配数据库中商品信 ...

  7. DataGridView单元格显示GIF图片

    本文转载:http://home.cnblogs.com/group/topic/40730.html DataGridView单元格显示GIF图片 gifanimationindatagrid.ra ...

  8. DataGridView 的单元格的边框、 网格线样式的设定【转】

    1) DataGridView 的边框线样式的设定DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的. BorderStyle 属性设定值是 ...

  9. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

随机推荐

  1. c# sql省市联动

    C# sql省市联动 USE [Book] GO ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ...

  2. GPU,CUDA,cuDNN的理解

    最近用到这方面的知识,感觉这篇文章写的很好,为了方便自己查阅,就搬运了过来,如果牵涉到侵权,请联系我,我会删除该博文!!! 我们知道做深度学习离不开GPU,不过一直以来对GPU和CPU的差别,CUDA ...

  3. windows下启动Apache报443错误!

    windows下启动apache报make_sock: could not bind to address [::]:443错误! 查看指定端口的占用情况 netstat -aon|findstr & ...

  4. Python学习第三方库Requests: 让 HTTP 服务人类

    转自官方文档:http://cn.python-requests.org/zh_CN/latest/ 快速上手 http://cn.python-requests.org/zh_CN/latest/u ...

  5. CENTOS 7 和 JDK 添加中文字体

    写在前面的话 当运维总是遇到各种奇奇怪怪的问题,比如新的 JAVA 项目上线,login 界面有个验证码,结果部署后发现,要么显示的奇奇怪怪,要么压根不显示. 或者在使用一些开源的 JAVA 项目的时 ...

  6. Summer Holiday 强连通

    Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity i ...

  7. JSONArray.fromObject Date显示问题

    原文链接:http://www.cnblogs.com/Nbge/archive/2012/07/31/2617127.html 使用JSONArray.fromObject,Date类型打出来的完全 ...

  8. left jion on和where条件的区别

    1.on是在生成临时表时()起作用,而且不管on中的条件是否为真,都会返回(left join)左边所有的数据,如果不匹配也是返回空. 2.where 是在生成了临时表后,再对表进行过滤 个人理解:先 ...

  9. 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国

    SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...

  10. p2p-如何拯救k8s镜像分发的阿喀琉斯之踵?

    K8s的出现为PaaS行业的发展打了一针兴奋剂,Docker+k8s的技术路线已经成为了容器云的主流.尤其针对大流量,大弹性的应用场景来说,k8s将其从繁杂的运维.部署工作中彻底拯救出来.然而事情往往 ...