目的:
擴展 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