好久没有写博客了,最近做了点小东西,分享给大家。DataGridView不像asp.net那样做页脚统计呢。所以呢,只有在数据源上就把页脚统计的信息加上去,这样才能有页脚统计的效果。
private void BindDataOnGrid()
...{
dsProductTableAdapters.ProductsTableAdapter adtProduct = new GetTotalAtFooter.dsProductTableAdapters.ProductsTableAdapter();
dsProduct.ProductsDataTable productTable = adtProduct.GetProducts();
dsProduct.ProductsRow pr = productTable.NewProductsRow();
pr.ProductName = "总计:";
decimal totalPrice = 0M;
foreach (dsProduct.ProductsRow dpr in productTable.Rows)
...{
totalPrice += dpr.UnitPrice;
}
//pr.UnitPrice = Convert.ToDecimal(adtProduct.GetSumUnitPrice());//用sql的sum函数在数据集里面进行计算
pr.UnitPrice = totalPrice;//用程序的方法计算DataTable里面的值
pr.Discontinued = false;
productTable.Rows.InsertAt(pr, productTable.Rows.Count);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = productTable;
}上面的ProductTable是怎么生成的,参看Scoot的三层结构的文章。
本文介绍了一种在DataGridView中实现页脚统计的方法。通过在数据源级别添加一条汇总记录,并利用C#代码计算总价格,实现了类似ASP.NET的页脚统计效果。
6747

被折叠的 条评论
为什么被折叠?



