GridView加入自动求和求平均值小计[转]

GridView加入自动求和求平均值小计

效果图:
解决方案:

1private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
2protected 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}
后台代码
前台:唯一的花头就是设置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>

 

--------------------------------------------------------------------------------------------------------

转自:http://www.cnblogs.com/longshengqun/archive/2008/09/22/1296275.html

--------------------------------------------------------------------------------------------------------

posted @ 2008-12-10 14:06  UouHt  阅读(960)  评论(0编辑  收藏  举报