Skip to main content

How to: Add Custom Holidays

  • 4 minutes to read

The following example demonstrates how to add custom holidays to your scheduling application. For this you need to create a Holiday object for every custom holiday, and add it to the SchedulerControl.WorkDays collection.

The code below demonstrates how to define a collection of custom holidays in the USA for 2007, and how to add them to the Scheduler Control.

using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Drawing;
// ...

    // A collection of US Holidays for 2007.
    private Holiday[] USHolidays2007 = { 
        new Holiday(new DateTime(2007, 1, 1), "New Year's Day"),
        new Holiday(new DateTime(2007, 1, 15), "Martin Luther King Jr.'s Birthday"),
        new Holiday(new DateTime(2007, 2, 19), "Presidents' Day"),
        new Holiday(new DateTime(2007, 4, 6), "Good Friday"),
        new Holiday(new DateTime(2007, 5, 28), "Memorial Day"),
        new Holiday(new DateTime(2007, 7, 4), "Independence Day"),
        new Holiday(new DateTime(2007, 9, 3), "Labor Day"),
        new Holiday(new DateTime(2007, 11, 22), "Thanksgiving Day"),
        new Holiday(new DateTime(2007, 12, 25), "Christmas Day")
    };

    // This method adds all US Holidays from the USHolidays2007 collection
    // to the WorkDays collection of the Scheduler Control.
    private void GenerateHolidaysFor2007() {
        foreach (Holiday item in USHolidays2007) {
            this.schedulerControl1.WorkDays.Add(item);
        }
    }

For example, these holidays may be added when a form containing a Scheduler Control is loaded.

private void Form1_Load(object sender, EventArgs e) {
        GenerateHolidaysFor2007();
    }

When a holiday is added to the SchedulerControl.WorkDays collection, the Date Navigator shows these holidays in Red (if its DateNavigator.HighlightHolidays property is set to true). In addition, it may be required to mark holidays when they are displayed within a Scheduler Control. For example, you may handle the SchedulerControl.CustomDrawDayHeader event, and add the holiday’s name to the header’s caption, as shown in the code below.

private void schedulerControl1_CustomDrawDayHeader(object sender, 
CustomDrawObjectEventArgs e) {
    // Check whether the current object is a Day Header.
    SchedulerHeader header = e.ObjectInfo as SchedulerHeader;
    if (header != null) {

        // Check whether the current date is a known holiday.
        Holiday hol = FindHoliday(header.Interval.Start.Date);
        if (hol != null) {

            // Add the holiday name to the day header's caption.
            header.Caption = String.Format("{0} ({1})", hol.DisplayName, 
                hol.Date.ToShortDateString());
        }
    }
}

// This method finds a holiday for the specified date.
private Holiday FindHoliday(DateTime date) {
    foreach (WorkDay item in schedulerControl1.WorkDays) {
        if (item is Holiday) {
            Holiday hol = (Holiday)item;
            if (hol.Date == date)
                return hol;
        }
    }
    return null;
}