Q

How do I write a calendar control that allows specification of start/end date from the database?

I want to write my own calendar control like where I will specify start date and end date from database. Is this possible? If yes, can you send me code?
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

Dig deeper on .NET Framework database programming best practices

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close