The procedure basically involves inheriting from Calendar and attaching to the SelectionChanged and VisibleMonthChanged events, where you can compare the new values with your DB-valid dates and set the VisibleDate/SelectedDate properties accordingly. You can also catch the DayRender and make the invalid dates unselectable, such as:
public class MyCalendar : Calendar
{
//Get the dates from a db connection.
DateTime _last = new DateTime(2003, 7, 30);
DateTime _first = new DateTime(2003, 4, 1);
public MyCalendar()
{
base.SelectionChanged += new EventHandler(OnDateChange);
base.VisibleMonthChanged += new MonthChangedEventHandler
(OnMonthChanged);
base.DayRender += new DayRenderEventHandler(OnDayRender);
}
private void OnDateChange(object sender, EventArgs e)
{
//Check valid date or show last/first valid ones.
if (SelectedDate > _last)
{
SelectedDate = _last;
}
else if (SelectedDate < _first)
{
SelectedDate = _first;
}
}
private void OnMonthChanged(object sender, MonthChangedEventArgs e)
{
//Check valid month or show last/first valid ones.
if (e.NewDate > _last || e.NewDate < _first)
{
VisibleDate = e.PreviousDate;
}
}
private void OnDayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date > _last || e.Day.Date < _first)
{
e.Day.IsSelectable = false;
}
}
}
This was first published in June 2003