目的:
扩展 c# winform 自带的表格控件,使其可以自动判断数据的上下界限值,并标识溢出。
这里使用的方法是:扩展 表格的列 对象:datagridviewcolumn。
1.创建类:decimalcheckcell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/// <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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/// <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 控件,添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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/archive/2017/08/13/7354057.html