目的:

扩展 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. 哈雷监控设备的操作及升级NSG9k6G

    哈雷监控设备的操作及升级NSG9k6G 一.下载升级包: http://pan.baidu.com/s/1kTmw9sr 如连接不可以用可以直接私聊我.QQ1841031740 二.升级: 下载完后, ...

  2. (字符转化)UTF-8和GBK有什么区别?

    GBK包含全部中文字符:UTF-8则包含全世界所有国家需要用到的字符. GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字 ...

  3. 51nod1448(yy)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1448 题意: 中文题诶~ 不过要仔细看题, 原来颜色是被覆盖 ...

  4. Mouse Hunt

    Mouse Hunt 给定一个n个点的图,每个点有权值\(c_i\),并且只有一条出边.现在你要在一些点上打标记,使得从任何一个点出发最终都会经过有标记的点.求标记点的权值和最小值. 就是找环啊!拓扑 ...

  5. 老男孩Day7作业:选课系统

    1.作业需求:角色:学校.学员.课程.讲师 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 3. 课程包 ...

  6. flink学习笔记-快速生成Flink项目

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  7. P3240 [HNOI2015]实验比较 树形DP

    \(\color{#0066ff}{ 题目描述 }\) 小D 被邀请到实验室,做一个跟图片质量评价相关的主观实验.实验用到的图片集一共有 \(N\) 张图片,编号为 \(1\) 到\(N\).实验分若 ...

  8. throw new Error('Cyclic dependency' + nodeRep)

    近日重装node_modules 依赖之后,项目启动报错 throw new Error('Cyclic dependency' + nodeRep) 查找资料后得知 产生这个 bug 的原因是循环引 ...

  9. HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

    HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.e ...

  10. vue.js路由嵌套

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...