it167.com  设为主页
 收藏本站
 
  资讯:业界动态 | 软件动态 | 人物专栏 | 安全资讯 | 网络生活 | 电子商务 | 小游戏 | 视频 | 美女图片 | 音乐
  网络编程 | 网站运营 | 网页制作 | 图形图象 | 操作系统 | 媒体动画 | 软件教学 | 网络应用 | 邮件系统 | 网络安全 | 认证考试
asp | .net | php | jsp | Sql | java | Dreamweaver | FrontPages | Javascript | css | Coreldraw | photoshop | Flash | Coreldraw
当前位置: > 主页>网络编程>Asp.net>Asp.Net实例教程>GridView加入自动求和求平均值小计
最新新闻

·机会与整合 边缘化互联
·TOM-Skype新增三大本地
·雅虎抢闸邮箱竞赛 网易
·新浪抢攻北京奥运
·洞悉网络口碑的掘金机会
·拆解网络病毒黑金交易
·木马下载器近期出现新变
·《互联网周刊》第17期文
·Web2.0是否催生自吹自擂
·三张宝宝裸照招来MSN封
热门新闻
·ASP.NET 2.0无刷新页面
·在ASP.NET中跨页面实现
·一个功能齐全的DataGrid
·ASP.NET 程序中常用的三
·GridView无代码分页排序
·GridView和下拉菜单Drop
·GridView 72般绝技1
·GridView和CheckBox结合
·鼠标移到GridView某一行
·鼠标移到GridView某一行
推荐新闻
 
 

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

作者:   来源:   点击:   日期:2007-11-27

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

效果图:

解决方案:    

private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是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=北风贸易;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 飞狐工作室";

        sqlcon = new SqlConnection(strCon);

        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

        DataSet myds = new DataSet();

        sqlcon.Open();

        myda.Fill(myds, "飞狐工作室");

        GridView1.DataSource = myds;

        GridView1.DataKeyNames = new string[] { "身份证号码" };

        GridView1.DataBind();

        sqlcon.Close();

    }

    private double sum = 0;//取指定列的数据和

    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();

           

        }

    }

}

前台:唯一的花头就是设置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>



文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·GridView数据导入Excel/Excel数据读入Gri ·GridView突出显示某一单元格(例如金额低
·GridView选中,编辑,取消,删除 ·GridView合并表头多重表头无错完美版(以
·GridView固定表头(不用javascript只用CSS ·GridView弹出新页面/弹出制定大小位置新

   文章评论:(0条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:it167  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。