1
Public Class DataGridViewSumRow
2
{
3
private
DataTable dt;
4
DataTable dtSum;
5
//
'行高
6
private
int
ROW_HEIGHT
=
21
;
7
8
private
void
DataGridViewSumRow_Load(System.Object sender, System.EventArgs e)
9
{
10
VScrollBar1.Visible
=
false
;
11
}
12
13
private
void
Button1_Click(System.Object sender, System.EventArgs e)
14
{
15
dt
=
GetData();
16
this
.DataGridView1.DataSource
=
dt;
17
this
.DataGridView1.RowTemplate.Height
=
ROW_HEIGHT;
18
19
GetSumData();
20
21
//
'行数超过当前页显示时显示纵向滚动条
22
23
if
(dt.Rows.Count
>
13
) {
24
VScrollBar1.Visible
=
true
;
25
//
'总长度为 (所有行数 - 画面一页显示行数) × 行高
26
VScrollBar1.Maximum
=
(
this
.DataGridView1.Rows.Count
-
this
.DataGridView1.DisplayedRowCount(
false
))
*
ROW_HEIGHT;
27
VScrollBar1.Minimum
=
0
;
28
VScrollBar1.SmallChange
=
21
;
29
VScrollBar1.LargeChange
=
50
;
30
31
}
32
}
33
34
///
<summary>
35
///
合计取得设定
36
///
</summary>
37
///
<remarks></remarks>
38
private
void
GetSumData()
39
{
40
DataRow dr
=
null
;
41
42
dtSum
=
new
DataTable(
"
TEST
"
);
43
44
dtSum
=
dt.Clone;
45
46
Random rdm
=
new
Random();
47
dr
=
dtSum.NewRow();
48
dr(
0
)
=
"
合计
"
;
49
50
for
(
int
i
=
1
; i
<=
dt.Columns.Count
-
1
; i
++
) {
51
dr(i)
=
dt.Compute(
"
Sum(
"
+
dt.Columns(i).ColumnName
+
"
)
"
,
"
true
"
);
52
}
53
dtSum.Rows.Add(dr);
54
55
this
.DataGridViewSum.DataSource
=
dtSum;
56
this
.DataGridViewSum.Rows(
0
).DefaultCellStyle.BackColor
=
Color.Brown;
57
this
.DataGridViewSum.ReadOnly
=
true
;
58
this
.DataGridViewSum.SelectionMode
=
DataGridViewSelectionMode.FullRowSelect;
59
60
}
61
62
///
<summary>
63
///
数据取得
64
///
</summary>
65
///
<returns></returns>
66
///
<remarks></remarks>
67
public
DataTable GetData()
68
{
69
DataTable dt
=
null
;
70
DataRow dr
=
null
;
71
72
dt
=
new
DataTable(
"
TEST
"
);
73
dt.Columns.Add(
new
DataColumn(
"
号码
"
,
typeof
(
string
)));
74
dt.Columns.Add(
new
DataColumn(
"
数量1
"
,
typeof
(
int
)));
75
dt.Columns.Add(
new
DataColumn(
"
数量2
"
,
typeof
(
int
)));
76
dt.Columns.Add(
new
DataColumn(
"
数量3
"
,
typeof
(
int
)));
77
dt.Columns.Add(
new
DataColumn(
"
数量4
"
,
typeof
(
int
)));
78
dt.Columns.Add(
new
DataColumn(
"
数量5
"
,
typeof
(
int
)));
79
dt.Columns.Add(
new
DataColumn(
"
数量6
"
,
typeof
(
int
)));
80
dt.Columns.Add(
new
DataColumn(
"
数量7
"
,
typeof
(
int
)));
81
dt.Columns.Add(
new
DataColumn(
"
数量8
"
,
typeof
(
int
)));
82
dt.Columns.Add(
new
DataColumn(
"
数量9
"
,
typeof
(
int
)));
83
84
Random rdm
=
new
Random();
85
86
for
(
int
i
=
10
; i
<=
80
; i
++
) {
87
dr
=
dt.NewRow();
88
dr[
0
]
=
"
00
"
+
i.ToString();
89
for
(
int
j
=
1
; j
<=
9
; j
++
) {
90
dr[j]
=
rdm.Next(
1000
,
5000
);
91
}
92
dt.Rows.Add(dr);
93
}
94
95
return
dt;
96
}
97
98
///
<summary>
99
///
纵滚动条事件
100
///
</summary>
101
///
<param name="sender"></param>
102
///
<param name="e"></param>
103
///
<remarks></remarks>
104
private
void
VScrollBar1_Scroll(
object
sender, System.Windows.Forms.ScrollEventArgs e)
105
{
106
this
.DataGridView1.FirstDisplayedScrollingRowIndex
=
e.NewValue;
107
//
ROW_HEIGHT
108
109
}
110
111
private
void
Button2_Click(System.Object sender, System.EventArgs e)
112
{
113
this
.Close();
114
}
115
116
///
<summary>
117
///
合计DataGridView的滚动条事件
118
///
</summary>
119
///
<param name="sender"></param>
120
///
<param name="e"></param>
121
///
<remarks></remarks>
122
123
private
void
DataGridViewSum_Scroll(
object
sender, System.Windows.Forms.ScrollEventArgs e)
124
{
125
this
.DataGridView1.HorizontalScrollingOffset
=
e.NewValue;
126
}
127
128
129
///
<summary>
130
///
数据变更后重新合计
131
///
</summary>
132
///
<param name="sender"></param>
133
///
<param name="e"></param>
134
///
<remarks></remarks>
135
private
void
DataGridView1_CellValueChanged(
object
sender, System.Windows.Forms.DataGridViewCellEventArgs e)
136
{
137
if
(e.ColumnIndex
<=
0
) {
138
return
;
139
}
140
//
'DetaGridView中数据变化后重新合计
141
this
.DataGridViewSum(e.ColumnIndex,
0
).Value
=
dt.Compute(
"
Sum(
"
+
dt.Columns(e.ColumnIndex).ColumnName
+
"
)
"
,
"
true
"
);
142
}
143
144
145
///
<summary>
146
///
响应鼠标滚轴事件
147
///
</summary>
148
///
<param name="sender"></param>
149
///
<param name="e"></param>
150
///
<remarks></remarks>
151
private
void
DataGridViewSumRow_MouseWheel(
object
sender, System.Windows.Forms.MouseEventArgs e)
152
{
153
if
(VScrollBar1.Visible
==
false
) {
154
return
;
155
}
156
157
if
(VScrollBar1.Value
-
ROW_HEIGHT
<
0
&
e.Delta
>
0
) {
158
VScrollBar1.Value
=
VScrollBar1.Minimum;
159
return
;
160
}
161
162
if
(VScrollBar1.Value
+
ROW_HEIGHT
>
VScrollBar1.Maximum
&
e.Delta
<
0
) {
163
VScrollBar1.Value
=
VScrollBar1.Maximum;
164
return
;
165
}
166
167
try
{
168
VScrollBar1.Value
-=
Convert.ToInt32(e.Delta
/
Math.Abs(e.Delta)
*
ROW_HEIGHT);
169
}
catch
(Exception ex) {
170
Debug.WriteLine(
"
###########################
"
);
171
Debug.WriteLine(
"
VScrollBar1.Value =
"
+
VScrollBar1.Value);
172
Debug.WriteLine(
"
e.Delta =
"
+
e.Delta);
173
Debug.WriteLine(
"
###########################
"
);
174
}
175
this
.DataGridView1.FirstDisplayedScrollingRowIndex
=
VScrollBar1.Value;
176
//
ROW_HEIGHT
177
178
}
179
}