Silverlight.DataSet 列名不能为数字
#region CreateBudgetSumDataSet
/// <summary>
/// 根据月份生成月份统计汇总
/// </summary>
/// <param name="monthList"></param>
/// <returns></returns>
private Silverlight.DataSet CreateBudgetSumDataSet(List<DateTime> monthList, List<int> currencyidList
, List<BudgetItem> budgetItemLis)
{
Silverlight.DataSet sumDs = new Silverlight.DataSet();
Silverlight.DataTable tbl = new Silverlight.DataTable("summary");
//Create Currency Column
Silverlight.DataColumn column = new Silverlight.DataColumn("currency",typeof(string));
column.Caption = "Currency";
tbl.Columns.Add(column);
//创建month列
foreach (DateTime month in monthList)
{
Silverlight.DataColumn newCol = new Silverlight.DataColumn(month.Year + "_" + month.Month);
newCol.Caption = string.Format("{0}/{1}", month.Year, month.Month);
tbl.Columns.Add(newCol);
}
sumDs.Tables.Add(tbl);
//生成统计行
foreach (int currencyid in currencyidList)
{
Silverlight.DataRow newRow = tbl.NewRow();
Currency currency = SLClient.BLL.Helper.Settings.BasicDataCollections.ListCollection.CurrencyList
.Single(x => { return x.ID == currencyid; });
newRow["currency"] = currency.Name;
foreach (DateTime month in monthList)
{
newRow[month.Year + "_" + month.Month] = this.CalulateSumBy(currencyid, month, budgetItemLis).ToString();
}
tbl.Rows.Add(newRow);
}
return sumDs;
}
#endregion
#region CalulateSumBy
/// <summary>
/// 根据日期和Currencyid 进行统计汇总
/// </summary>
/// <param name="currencyid"></param>
/// <param name="date"></param>
/// <param name="budgetItemList"></param>
/// <returns></returns>
private decimal CalulateSumBy(int currencyid, DateTime date, List<BudgetItem> budgetItemList)
{
decimal result = decimal.Zero;
budgetItemList.ForEach(item =>
{
if (item.Currencyid == item.Currencyid
&& MonthComparer.IsInSameMonth(item.Availabledate, date))
{
result += item.Price;
}
});
return result;
}
#endregion
#region PopulateSummary
/// <summary>
/// 重新计算列表并显示
/// </summary>
/// <param name="BudgetItemList"></param>
public void PopulateSummary(List<BudgetItem> budgetItemList)
{
List<DateTime> monthList = new List<DateTime>();
List<int> currencyIdList = new List<int>();
foreach (BudgetItem item in budgetItemList)
{
if (!monthList.Contains(item.Availabledate, new MonthComparer())
)
{
monthList.Add(item.Availabledate);
}
if (!currencyIdList.Contains(item.Currencyid))
{
currencyIdList.Add(item.Currencyid);
}
}
//Silverlight.DataSet ds = CreateBudgetSumDataSet(monthList,currencyIdList,budgetItemList);
Silverlight.DataSet ds = this.CreateTestDataSet();
this.dgItems.ItemsSource = ds.Tables[0].GetBindableData(this._connector);
}
#endregion
#region MonthComparer
/// <summary>
/// 月份比较类 ,判读两个日期是否在相同的年月内
/// </summary>
class MonthComparer : IEqualityComparer<DateTime>
{
#region IEqualityComparer<DateTime> Members
public bool Equals(DateTime x, DateTime y)
{
return IsInSameMonth(x, y);
}
public int GetHashCode(DateTime obj)
{
throw new NotImplementedException();
}
#endregion
#region IsInSameMonth
/// <summary>
/// 判断两个日期是否同年同月
/// </summary>
/// <param name="date1"></param>
/// <param name="date2"></param>
/// <returns></returns>
public static bool IsInSameMonth(DateTime date1, DateTime date2)
{
if (date1.Year == date2.Year
&& date1.Month == date1.Month)
{
return true;
}
return false;
}
#endregion
}
#endregion