在用ObjectDataSource数据源分页 统计有多少条数据或者合计某个单价的时候,总是显示当前页面数据的合,点下一页就是下一页的合,所以我就写了这样一个类。 以便以后复制
代码
/// <summary>
/// 统计label
/// </summary>
private Label countLabel;
/// <summary>
/// 计算Lable
/// </summary>
private Label sumLabel;
private Dictionary<string, Label> sumLabelDic = new Dictionary<string, Label>();
/// <summary>
/// 计算列名
/// </summary>
private string sumCellName;
#region
/// <summary>
/// 统计ObjectDataSource为数据源的GridView
/// </summary>
/// <param name="ods">要统计GridView的ObjectDataSource</param>
/// <param name="label">显示统计结果的Label</param>
protected void CountODSGridView(ObjectDataSource ods, Label label)
{
countLabel = label;
ods.Selected += new ObjectDataSourceStatusEventHandler(ObjectDataSourceSelectedCount);
//页面初始化赋值委托
//this.Load += PageLoadForSumAndCount;
}
/// <summary>
/// 计算ObjectDataSource为数据源的GridView
/// </summary>
/// <param name="ods">要计算GridView的ObjectDataSource</param>
/// <param name="label">显示计算结果的Label</param>
/// <param name="cellName">计算列名</param>
protected void SumODSGridView(ObjectDataSource ods, Label label, string cellName)
{
if (sumLabelDic.Keys.Count == 0)
{
ods.Selected += new ObjectDataSourceStatusEventHandler(ObjectDataSourceSelectedSum);
}
sumLabelDic.Add(cellName, label);
sumLabel = label;
sumCellName = cellName;
//页面初始化赋值委托
//this.Load += PageLoadForSumAndCount;
}
///// <summary>a
///// 为统计和求和做页面加载
///// </summary>
///// <param name="sender"></param>
///// <param name="e"></param>
//public void PageLoadForSumAndCount(object sender, EventArgs e){
// if (sumLabel!=null)
// {
// sumLabel.Text = "0.00";
// }
// if (countLabel!=null)
// {
// countLabel.Text = "0";
// }
//}
/// <summary>
/// 统计委托
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ObjectDataSourceSelectedCount(Object sender, ObjectDataSourceStatusEventArgs e)
{
int count = 0;
if (e.ReturnValue != null)
{
DataTable dt = (DataTable)e.ReturnValue;
count = dt.Rows.Count;
}
countLabel.Text = count.ToString();
}
/// <summary>
/// 计算委托
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ObjectDataSourceSelectedSum(Object sender, ObjectDataSourceStatusEventArgs e)
{
foreach (string cellName in sumLabelDic.Keys)
{
string sum = "";
if (e.ReturnValue != null)
{
DataTable dt = (DataTable)e.ReturnValue;
foreach (DataRow row in dt.Rows)
{
object cell = row[cellName];
if (cell != null)
{
sum += cell + ",";
}
}
}
sumLabelDic[cellName].Text = sum.ToString();
}
// sumLabel.Text = sum.ToString();
}
#endregion
}