首页 新闻 赞助 找找看

GridView的统计分组排序问题

0
悬赏园豆:15 [已解决问题] 解决于 2012-04-29 23:31

最终目的:NO分组,统计Fee;以下代码,后台绑定,不能实现如图统计效果

 
        if (!Page.IsPostBack)
        {
            string con = "server=.;database=dormitory;uid=sa;pwd=;";
            string sql = "select No,Name,Fee from DorFee";

            SqlConnection sqlCon = new SqlConnection(con);
            sqlCon.Open();
            SqlCommand sqlCom = new SqlCommand(sql, sqlCon);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = sqlCom;
            DataSet ds = new DataSet();

            sda.Fill(ds);
            sqlCon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();

            GridViewHelper gh = new GridViewHelper(this.GridView1);
            gh.RegisterSummary("Fee", SummaryOperation.Sum);
            
        }

对GridView中的数据进行分组排序,如果在前台的控件上进行绑定DB后,借助GridViewHelper对GridView中的数据进行分组排序可以实现(最后的两行代码),但是在后台对gridview绑定数据后无法实现如图效果的统计效果。 前台的控件绑定和后台的bind()有区别吗???GridViewHelper  类http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm

后台绑定注释掉,只留最后现行;从前台绑定GridViewHelper是可以实现统计如上图,前台:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DormitoryConnectionString %>" 
            SelectCommand="SELECT * FROM [DorFee]"></asp:SqlDataSource>


 

hishanghai的主页 hishanghai | 初学一级 | 园豆:163
提问于:2012-04-21 18:25
< >
分享
最佳答案
0

数据查询出来后,判断计算出所需的有效数据,然后存放到DataTable中,借用DataTable的Compute方法进行分组统计,然后把数据绑定GridView后显示。

问题已经解决,但是这样在后台进行的计算很影响网页打开速度,要5秒后才能显示出来,

暂时用着,以后再用存储过程解决吧

hishanghai | 初学一级 |园豆:163 | 2012-04-25 19:11
其他回答(3)
0

有区别的,在后台绑定的一般都要特别注意在page_load中的处理,把你的代码关键部分写出来吧

收获园豆:5
artwl | 园豆:16736 (专家六级) | 2012-04-21 18:47
0
// Class-scope, running total variables...

decimal _totalUnitPrice = 0m;

int _totalNonNullUnitPriceCount = 0;

int _totalUnitsInStock = 0;

int _totalUnitsOnOrder = 0;



protected void ProductsInCategory_RowDataBound(object sender,

  GridViewRowEventArgs e)

{

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

    {

        // Reference the ProductsRow via the e.Row.DataItem property

        Northwind.ProductsRow product =

          (Northwind.ProductsRow)

          ((System.Data.DataRowView)e.Row.DataItem).Row;



        // Increment the running totals (if they are not NULL!)

        if (!product.IsUnitPriceNull())

        {

            _totalUnitPrice += product.UnitPrice;

            _totalNonNullUnitPriceCount++;

        }



        if (!product.IsUnitsInStockNull())

            _totalUnitsInStock += product.UnitsInStock;



        if (!product.IsUnitsOnOrderNull())

            _totalUnitsOnOrder += product.UnitsOnOrder;

    }

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

    {

      ... <I>Increment the running totals</I> ...

    }

    else if (e.Row.RowType == DataControlRowType.Footer)

    {

      // Determine the average UnitPrice

      decimal avgUnitPrice = _totalUnitPrice /

(decimal) _totalNonNullUnitPriceCount;
      // Display the summary data in the appropriate cells

      e.Row.Cells[1].Text = "Avg.: " + avgUnitPrice.ToString("c");

      e.Row.Cells[2].Text = "Total: " + _totalUnitsInStock.ToString();

      e.Row.Cells[3].Text = "Total: " + _totalUnitsOnOrder.ToString();

    }


}
收获园豆:5
悟行 | 园豆:12559 (专家六级) | 2012-04-21 20:08

GridViewHelper 类,如果直接再aspx页面对控件绑定可以用这个,但是后台的代码绑定就不行了,一直没有实现……

支持(0) 反对(0) hishanghai | 园豆:163 (初学一级) | 2012-04-23 19:24
0
收获园豆:5
邀月 | 园豆:25475 (高人七级) | 2012-04-21 20:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册