最终目的: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>
数据查询出来后,判断计算出所需的有效数据,然后存放到DataTable中,借用DataTable的Compute方法进行分组统计,然后把数据绑定GridView后显示。
问题已经解决,但是这样在后台进行的计算很影响网页打开速度,要5秒后才能显示出来,
暂时用着,以后再用存储过程解决吧
有区别的,在后台绑定的一般都要特别注意在page_load中的处理,把你的代码关键部分写出来吧
// 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(); } }
GridViewHelper 类,如果直接再aspx页面对控件绑定可以用这个,但是后台的代码绑定就不行了,一直没有实现……