zoukankan      html  css  js  c++  java
  • GridView中列中数据的汇总

            

     在部分项目中我们用到GridView控件会涉及到列中数据统计,下面针对GridView中列的汇总做以说明;

    1.       开始我们以northwind数据库为例从数据库中读取数据到页面

       String connString = "server=.;user=sa;pwd=;database=northwind";

        //汇总价格

       double totalPrice = 0;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                using (SqlConnection sqlCon = new SqlConnection(connString))

                {

                    String sql = "select top 10 productid,productname,unitprice from products";

                    using (SqlCommand cmd = new SqlCommand(sql, sqlCon))

                    {

                        sqlCon.Open();

                        SqlDataReader rs = cmd.ExecuteReader();

                        gvProducts.DataSource = rs;

                        gvProducts.DataBind();

                    }

                }

            }

    }

     

    2.       接下来书写OnRowDataBound 事件(在对行进行了数据绑定后激发

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                double price = double.Parse(e.Row.Cells[e.Row.Cells.Count - 1].Text);

                totalPrice += price;

            }

    3.       DataBound事件(控件被数据绑定后事件)

    gvProducts.FooterRow.Cells[gvProducts.FooterRow.Cells.Count - 1].Text = totalPrice.ToString();

  • 相关阅读:
    C语言-第四周作业
    第8次Scrum会议(10/20)【欢迎来怼】
    例行报告(20171011-20171019)
    C语言--第二周作业评分和总结(5班)
    C语言-第三周作业
    第一次Scrum会议(10/13)【欢迎来怼】
    单元测试之四则运算
    四则运算V1.1
    例行报告(20170927-20171010)
    C语言--第一周作业评分和总结(5班)
  • 原文地址:https://www.cnblogs.com/tanliang/p/1679459.html
Copyright © 2011-2022 走看看