★搜Asp.net★(
www.soAsp.net
),为专业技术文档网站。
包括Asp.net开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档·VB.NET开发技术文档。
还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。
GridView加入自动求和求平均值小计
GridView加入自动求和求平均值小计
效果图:
解决方案:
1
private
double
sum
=
0
;
//
取指定列的数据和,你要根据具体情况对待可能你要处理的是int
2
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
3
{
4
if
(e.Row.RowIndex
>=
0
)
5
{
6
sum
+=
Convert.ToDouble(e.Row.Cells[
6
].Text);
7
}
8
else
if
(e.Row.RowType
==
DataControlRowType.Footer)
9
{
10
e.Row.Cells[
5
].Text
=
"
总薪水为:
"
;
11
e.Row.Cells[
6
].Text
=
sum.ToString();
12
e.Row.Cells[
3
].Text
=
"
平均薪水为:
"
;
13
e.Row.Cells[
4
].Text
=
((
int
)(sum
/
GridView1.Rows.Count)).ToString();
14
}
15
}
后台全部代码:
1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Web;
5
using
System.Web.Security;
6
using
System.Web.UI;
7
using
System.Web.UI.WebControls;
8
using
System.Web.UI.WebControls.WebParts;
9
using
System.Web.UI.HtmlControls;
10
using
System.Data.SqlClient;
11
using
System.Drawing;
12
public
partial
class
Default7 : System.Web.UI.Page
13
{
14
SqlConnection sqlcon;
15
SqlCommand sqlcom;
16
string
strCon
=
"
Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa
"
;
17
protected
void
Page_Load(
object
sender, EventArgs e)
18
{
19
if
(
!
IsPostBack)
20
{
21
bind();
22
}
23
}
24
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
25
{
26
GridView1.EditIndex
=
e.NewEditIndex;
27
bind();
28
}
29
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
30
{
31
sqlcon
=
new
SqlConnection(strCon);
32
string
sqlstr
=
"
update 飞狐工作室 set 姓名='
"
33
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[
1
].Controls[
0
])).Text.ToString().Trim()
+
"
',家庭住址='
"
34
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[
3
].Controls[
0
])).Text.ToString().Trim()
+
"
' where 身份证号码='
"
35
+
GridView1.DataKeys[e.RowIndex].Value.ToString()
+
"
'
"
;
36
sqlcom
=
new
SqlCommand(sqlstr, sqlcon);
37
sqlcon.Open();
38
sqlcom.ExecuteNonQuery();
39
sqlcon.Close();
40
GridView1.EditIndex
=
-
1
;
41
bind();
42
}
43
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
44
{
45
GridView1.EditIndex
=
-
1
;
46
bind();
47
}
48
public
void
bind()
49
{
50
string
sqlstr
=
"
select top 5 * from 飞狐工作室
"
;
51
sqlcon
=
new
SqlConnection(strCon);
52
SqlDataAdapter myda
=
new
SqlDataAdapter(sqlstr, sqlcon);
53
DataSet myds
=
new
DataSet();
54
sqlcon.Open();
55
myda.Fill(myds,
"
飞狐工作室
"
);
56
GridView1.DataSource
=
myds;
57
GridView1.DataKeyNames
=
new
string
[]
{
"
身份证号码
"
}
;
58
GridView1.DataBind();
59
sqlcon.Close();
60
}
61
private
double
sum
=
0
;
//
取指定列的数据和
62
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
63
{
64
if
(e.Row.RowIndex
>=
0
)
65
{
66
sum
+=
Convert.ToDouble(e.Row.Cells[
6
].Text);
67
}
68
else
if
(e.Row.RowType
==
DataControlRowType.Footer)
69
{
70
e.Row.Cells[
5
].Text
=
"
总薪水为:
"
;
71
e.Row.Cells[
6
].Text
=
sum.ToString();
72
e.Row.Cells[
3
].Text
=
"
平均薪水为:
"
;
73
e.Row.Cells[
4
].Text
=
((
int
)(sum
/
GridView1.Rows.Count)).ToString();
74
}
75
}
76
}
前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
CellPadding
="3"
OnRowEditing
="GridView1_RowEditing"
2
OnRowUpdating
="GridView1_RowUpdating"
OnRowCancelingEdit
="GridView1_RowCancelingEdit"
BackColor
="White"
BorderColor
="#CCCCCC"
BorderStyle
="None"
BorderWidth
="1px"
Font-Size
="12px"
OnRowDataBound
="GridView1_RowDataBound"
ShowFooter
="True"
>
3
<
FooterStyle
BackColor
="White"
ForeColor
="#000066"
/>
4
<
Columns
>
5
<
asp:CommandField
HeaderText
="编辑"
ShowEditButton
="True"
/>
6
<
asp:BoundField
DataField
="身份证号码"
HeaderText
="编号"
ReadOnly
="True"
/>
7
<
asp:BoundField
DataField
="姓名"
HeaderText
="姓名"
/>
8
<
asp:BoundField
DataField
="出生日期"
HeaderText
="邮政编码"
/>
9
<
asp:BoundField
DataField
="家庭住址"
HeaderText
="家庭住址"
/>
10
<
asp:BoundField
DataField
="邮政编码"
HeaderText
="邮政编码"
/>
11
<
asp:BoundField
DataField
="起薪"
HeaderText
="起薪"
/>
12
13
</
Columns
>
14
<
RowStyle
ForeColor
="#000066"
/>
15
<
SelectedRowStyle
BackColor
="#669999"
Font-Bold
="True"
ForeColor
="White"
/>
16
<
PagerStyle
BackColor
="White"
ForeColor
="#000066"
HorizontalAlign
="Left"
CssClass
="ms-formlabel DataGridFixedHeader"
/>
17
<
HeaderStyle
BackColor
="#006699"
Font-Bold
="True"
ForeColor
="White"
/>
18
</
asp:GridView
>