//思路:新建dataTable,定义需要的列, 先将数据源进行分组,第一重遍历获取所有组,第二重遍历获取某一个组的具体数据
public void DataBind()
{
DateTime time;
if (string.IsNullOrEmpty(txtDate.Value.Trim()) || !DateTime.TryParse(txtDate.Value.Trim() + "-01", out time))
{
//如果时间为空,则查询当月
this.txtDate.Value = DateTime.Now.ToString("yyyy-MM");
}
//获取各部门对于各物流公司的费用统计
DataTable AllData = CurrentBll.GetLogisticsCompanyExpensesDt(this.txtDate.Value.Trim());
var data = from item in AllData.AsEnumerable()
group item by item["DeptCode"].DBValueToString();
//各部门数据
DataTable table = new DataTable();
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "DeptCode" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "shunfeng" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "zaiji" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "quanyi" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "cces" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "quanfeng" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "ems" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "shengtong" });
table.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "sum" });
foreach (IGrouping<string, DataRow> group in data)
{
DataRow row = table.NewRow();
row["DeptCode"] = group.Key;
foreach (var temp in group)
{
string cost = temp["Cost"].DBValueToString();
switch ( temp["DeliveryType"].DBValueToInt32() )
{
case CourierDeliveryType.SF: row["shunfeng"] = cost;
break;
case CourierDeliveryType.ZJS: row["zaiji"] = cost;
break;
case CourierDeliveryType.QY: row["quanyi"] = cost;
break;
case CourierDeliveryType.CCES: row["cces"] = cost;
break;
case CourierDeliveryType.QF: row["quanfeng"] = cost;
break;
case CourierDeliveryType.EMS: row["ems"] = cost;
break;
case CourierDeliveryType.ST: row["shengtong"] = cost;
break;
};
}
//统计该行的所有数据
row["sum"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
table.Rows.Add(row);
}
//合计信息
DataTable CountTable = table.Clone();
var count = from item in AllData.AsEnumerable()
group item by item["DeliveryType"].DBValueToInt32();
DataRow countRow = CountTable.NewRow();
//统计统计
countRow["sum"] = count.Sum(p => p.Sum(l => l["Cost"].DBValueToDecimal())).ToString();
foreach (IGrouping<int, DataRow> group in count)
{
switch (group.Key.DBValueToInt32())
{
case CourierDeliveryType.SF: countRow["shunfeng"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
case CourierDeliveryType.ZJS: countRow["zaiji"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
case CourierDeliveryType.QY: countRow["quanyi"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
case CourierDeliveryType.CCES: countRow["cces"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
case CourierDeliveryType.QF: countRow["quanfeng"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
case CourierDeliveryType.EMS: countRow["ems"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
case CourierDeliveryType.ST: countRow["shengtong"] = group.Sum(p => p["Cost"].DBValueToDecimal()).ToString();
break;
};
}
CountTable.Rows.Add(countRow);
this.rptLogisticsCompanyExpenses.DataSource = table;
this.rptLogisticsCompanyExpenses.DataBind();
this.rptSum.DataSource = CountTable;
this.rptSum.DataBind();
}