我需要根据从数据库中获得的数据突出显示 MonthCalendar 中的日期,但它不断向我抛出 OutOfMemory 异常。我得到了我必须从数据库中突出显示为 bool 值的日子,如下所示:
command.CommandText = "SELECT Monday FROM Days WHERE Event = "+idEvent+"";
Monday = Convert.ToBoolean(command.ExecuteScalar());
一周中的每一天都使用类似的代码。我必须突出显示日期的范围是整年(开始和结束是一年中的第一天和最后一天,都是 DateTime 类型)。我试图用这段代码做到这一点:
for (DateTime day = start; day < end; day.AddDays(1))
{
if (Monday && day.DayOfWeek == DayOfWeek.Monday)
{
dates.Add(day);
}
else if (Tuesday && day.DayOfWeek == DayOfWeek.Tuesday)
{
dates.Add(day);
}
else if (Wednesday && day.DayOfWeek == DayOfWeek.Wednesday)
{
dates.Add(day);
}
else if (Thursday && day.DayOfWeek == DayOfWeek.Thursday)
{
dates.Add(day);
}
else if (Friday && day.DayOfWeek == DayOfWeek.Friday)
{
dates.Add(day);
}
else if (Saturday && day.DayOfWeek == DayOfWeek.Saturday)
{
dates.Add(day);
}
else if (Sunday && day.DayOfWeek == DayOfWeek.Sunday)
{
dates.Add(day);
}
}
monthCalendar1.BoldedDates = dates.ToArray();
该算法的重点是粗体显示所选事件发生的日期(每个事件全年定期发生,除了一些例外,如假期或某些特定时间间隔)。因此,对于每个事件,每年可能会发生 200 多次。我怎么解决这个问题?