Gridview中实现求和统计功能
生活随笔
收集整理的這篇文章主要介紹了
Gridview中实现求和统计功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
GridView加入自動求和求平均值小計效果圖:解決方案:
private double sum = 0; //取指定列的數(shù)據(jù)和,你要根據(jù)具體情況對待可能你要處理的是int
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){if (e.Row.RowIndex >= 0){sum += Convert.ToDouble(e.Row.Cells[6].Text);}else if (e.Row.RowType == DataControlRowType.Footer){e.Row.Cells[5].Text = "總薪水為:";e.Row.Cells[6].Text = sum.ToString();e.Row.Cells[3].Text = "平均薪水為:";e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();}}
后臺全部代碼:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
public partial class Default7 : System.Web.UI.Page
{SqlConnection sqlcon;SqlCommand sqlcom;string strCon = "Data Source=(local);Database=北風貿(mào)易;Uid=sa;Pwd=sa";protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){bind();}}protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e){GridView1.EditIndex = e.NewEditIndex;bind();}protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){sqlcon = new SqlConnection(strCon);string sqlstr = "update 飛狐工作室 set 姓名='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份證號碼='"+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";sqlcom = new SqlCommand(sqlstr, sqlcon);sqlcon.Open();sqlcom.ExecuteNonQuery();sqlcon.Close();GridView1.EditIndex = -1;bind();}protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e){GridView1.EditIndex = -1;bind();}public void bind(){string sqlstr = "select top 5 * from 編程中國社區(qū)";sqlcon = new SqlConnection(strCon);SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);DataSet myds = new DataSet();sqlcon.Open();myda.Fill(myds, "編程中國社區(qū)");GridView1.DataSource = myds;GridView1.DataKeyNames = new string[] { "身份證號碼" };GridView1.DataBind();sqlcon.Close();}private double sum = 0; //取指定列的數(shù)據(jù)和protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){if (e.Row.RowIndex >= 0){sum += Convert.ToDouble(e.Row.Cells[6].Text);}else if (e.Row.RowType == DataControlRowType.Footer){e.Row.Cells[5].Text = "總薪水為:";e.Row.Cells[6].Text = sum.ToString();e.Row.Cells[3].Text = "平均薪水為:";e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();}}
}
前臺:唯一的花頭就是設(shè)置ShowFooter="True" ,否則默認表頭為隱藏的!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" ><FooterStyle BackColor="White" ForeColor="#000066" /><Columns><asp:CommandField HeaderText="編輯" ShowEditButton="True" /><asp:BoundField DataField="身份證號碼" HeaderText="編號" ReadOnly="True" /><asp:BoundField DataField="姓名" HeaderText="姓名" /><asp:BoundField DataField="出生日期" HeaderText="郵政編碼" /><asp:BoundField DataField="家庭住址" HeaderText="家庭住址" /><asp:BoundField DataField="郵政編碼" HeaderText="郵政編碼" /><asp:BoundField DataField="起薪" HeaderText="起薪" /></Columns><RowStyle ForeColor="#000066" /><SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/><HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /></asp:GridView>
轉(zhuǎn)載于:https://www.cnblogs.com/shangshen/p/3592197.html
總結(jié)
以上是生活随笔為你收集整理的Gridview中实现求和统计功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 8.1 新增控件之 Hy
- 下一篇: MVC中跳转到其他页面,并传参数