例如,我有这样的 DateTime
列表;
List<DateTime> timeList;
我使用 repeater 创建了一个如下所示的表格;
YEAR MONTH DATES
2012 SEPTEMBER 08 15 22 29
2012 OCTOBER 06 20 27
2012 NOVEMBER 10
2012 DECEMBER 08
2013 MAY 04 18
2013 JUNE 01 15 29
2013 JULY 13 27
2013 AUGUST 10 24
2013 SEPTEMBER 07 21
2013 OCTOBER 05 19
但我无法对日期进行分组。如何使用中继器实现此目的?
最佳答案
首先,创建一个类来保存你的数据:
public class DateInfo
{
public int Year { get; set; }
public string Month { get; set; }
public IEnumerable<int> Days { get; set; }
public string DisplayDayList
{
get
{
return string.Join(" ", Days.Select(x=>x.ToString()).ToArray()); //sorry, i'm doing .net 3.5
}
}
}
然后,您可以按年/月分组以绑定(bind)您的列表。
List<DateTime> dtList = new List<DateTime>();
List<DateInfo> dateInfoList =
(from dt in dtList
group dt by new { dt.Year, dt.Month } into g
select new DateInfo()
{
Year = g.Key.Year,
Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month),
Days = g.Select(x => x.Day)
}).ToList();
现在,使用您的新列表对象进行绑定(bind),并且只绑定(bind) Year
、Month
和 DisplayDayList
列。
关于c# - 如何安排日期时间列表的特殊表格格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16362984/