Gridview中加入求和和求平均值 (转载)

前台代码:

View Code
1 < asp:GridView ID = " GridView1 " ShowFooter = " true " runat = " server " AutoGenerateColumns = " false "
2 style = " border:solid 1px black " onrowdatabound = " GridView1_RowDataBound " >
3 < Columns >
4 < asp:BoundField DataField = " 编号 " HeaderText = " 编号 " />
5 < asp:BoundField DataField = " 姓名 " HeaderText = " 姓名 " />
6 < asp:BoundField DataField = " 邮政编码 " HeaderText = " 邮政编码 " />
7 < asp:BoundField DataField = " 家庭住址 " HeaderText = " 家庭住址 " />
8 < asp:TemplateField HeaderText = " 出生年月 " >
9 < ItemTemplate >
10 <% #Eval( " 出生年月 " , " {0:yyyy-MM-dd} " ) %>
11 </ ItemTemplate >
12 </ asp:TemplateField >
13 < asp:BoundField DataField = " 起薪 " HeaderText = " 起薪 " />
14 </ Columns >
15 </ asp:GridView >

后台代码:

View Code
1 protected void Page_Load( object sender, EventArgs e)
2 {
3 if ( ! IsPostBack)
4 {
5 databind();
6 }
7
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Conn " ].ToString());
12 SqlCommand cmd = new SqlCommand();
13 cmd.Connection = con;
14 cmd.CommandText = " select * from T_Message " ;
15 SqlDataAdapter da = new SqlDataAdapter(cmd);
16 DataSet ds = new DataSet();
17 da.Fill(ds);
18 this .GridView1.DataSource = ds.Tables[ 0 ];
19 this .GridView1.DataBind();
20 }
21 double sum = 0 ;
22 protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e)
23 {
24 foreach (TableCell item in e.Row.Cells)
25 {
26 item.Attributes.Add( " style " , " border-color:black " );
27 }
28
29 if (e.Row.RowType == DataControlRowType.DataRow)
30 {
31 sum = sum + Convert.ToInt32(e.Row.Cells[ 5 ].Text);
32 }
33 if (e.Row.RowType == DataControlRowType.Footer)
34 {
  e.Row.Cells[0].CssClass = "color";//添加外部样式
35 e.Row.Cells[ 4 ].Text = " 每月支出薪水 " ;
36 e.Row.Cells[ 2 ].Text = " 平均薪水 " ;
37 e.Row.Cells[ 3 ].Text = (sum / GridView1.Rows.Count).ToString();
38 e.Row.Cells[ 5 ].Text = sum.ToString();
39 }
40 }

转载于:https://www.cnblogs.com/zhang9418hn/archive/2012/01/11/2319661.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值