Back to Devexpress

Workweek Schedule and Exceptions

windowsforms-401291-controls-and-libraries-gantt-control-workweek-schedule-and-exceptions.md

latest12.8 KB
Original Source

Workweek Schedule and Exceptions

  • Oct 09, 2023
  • 7 minutes to read

The control allows you to customize the workweek schedule and exceptions. The workweek schedule is used to automatically reschedule tasks when a user modifies a particular task. See Interactive Editing for more information.

Workweek Schedule

The GanttControl.WorkWeek property — provides access to the workweek schedule. The default workweek is from Monday to Friday. Work hours are 8:00 to 17:00 with a break between 12:00 and 13:00.

To customize the schedule in the Visual Studio Designer, click Edit Work Week Schedule in the control’s smart tag menu (or click Run Designer and navigate to the Work Week Schedule section). You can specify the schedule for each day of the week.

The upper bound is not included in the working hours. For example, if the working time is until noon, 12:00 PM is not working time, but the time a moment (tick) earlier, remains the working time.

Note

The specified work hours are automatically sorted. If the specified work hours overlap each other, they are automatically merged.

The code below shows how to change work hours for Wednesday and make Monday a day off.

csharp
// Add Monday as a non-work day.
ganttControl1.WorkWeek.Add(new WorkDayOfWeek(DayOfWeek.Monday, true));

// Work hours for Wednesday are 9:00 to 17:30
// with a break between 12:00 and 13:30.
WorkTime wednesdayMorningWorkTime = new WorkTime(9, 12);
WorkTime wednesdayAfternoonWorkTime = new WorkTime(new TimeSpan(13, 30, 00), new TimeSpan(17, 30, 00));
WorkDayOfWeek wednesday = new WorkDayOfWeek(DayOfWeek.Wednesday, wednesdayMorningWorkTime, wednesdayAfternoonWorkTime);
ganttControl1.WorkWeek.Add(wednesday);
vb
' Add Monday as a non-work day.
ganttControl1.WorkWeek.Add(New WorkDayOfWeek(DayOfWeek.Monday, True))

' Work hours for Wednesday are 9:00 to 17:30
' with a break between 12:00 and 13:30.
Dim wednesdayMorningWorkTime As New WorkTime(9, 12)
Dim wednesdayAfternoonWorkTime As New WorkTime(New TimeSpan(13, 30, 00), New TimeSpan(17, 30, 00))
Dim wednesday As New WorkDayOfWeek(DayOfWeek.Wednesday, wednesdayMorningWorkTime, wednesdayAfternoonWorkTime)
ganttControl1.WorkWeek.Add(wednesday)

Exceptions

The GanttControl.Exceptions property — provides access to rules that specify exceptions to the regular workweek schedule. You can specify the following exceptions:

  • DailyExceptionRule — Represents a rule that specifies an exception that reoccurs every day.
  • DaysExceptionRule – Represents a rule that specifies an exception that reoccurs by days.
  • WeeklyExceptionRule — Represents a rule that specifies an exception that reoccurs every week on a particular day of the week.
  • MonthlyExceptionRule — Represents a rule that specifies an exception that reoccurs every month on a particular day of the month.
  • MonthlyDayOfWeekExceptionRule — Represents a rule that specifies an exception that reoccurs every month in a particular week of the month and day of the week.
  • YearlyExceptionRule — Represents a rule that specifies an exception that reoccurs every year in a particular month and day of the month.
  • YearlyDayOfWeekExceptionRule — Represents a rule that specifies an exception that reoccurs every year in a particular month, week of the month, and day of the week.
  • YearlyDayOfYearExceptionRule — Represents a rule that specifies an exception that reoccurs every year on a particular day of the year.

To customize exceptions in the Visual Studio Designer, click Edit Exceptions in the control’s smart tag menu (or click Run Designer and navigate to the Exceptions section).

The code below shows how to specify custom exception rules.

csharp
using DevExpress.XtraGantt;
using DevExpress.XtraGantt.Base.Scheduling;
using DevExpress.XtraGantt.Exceptions;
using DevExpress.XtraGantt.Scheduling;

private ExceptionRule[] CreateCustomRules() {

    DailyExceptionRule dailyException = new DailyExceptionRule() {
        StartDate = new DateTime(2019, 8, 19),
        Occurrences = 7
    };
    dailyException.WorkTimes.Add(new WorkTime(9, 12));

    DaysExceptionRule daysException = new DaysExceptionRule() {
        StartDate = new System.DateTime(2023, 9, 27, 0, 0, 0, 0),
        Occurrences = 1
    };

    // The rule applies to the first day of each month (January, 1; February, 1; etc.)
    MonthlyExceptionRule FirstDayInMonth = new MonthlyExceptionRule() {
        DayOfMonth = 1
    };
    FirstDayInMonth.Add(new WorkTime(10, 12));

    // The rule applies to the second Friday of each month.
    MonthlyDayOfWeekExceptionRule SecondFridayInMonth = new MonthlyDayOfWeekExceptionRule() {
        DayOfWeek = DayOfWeek.Friday,
        WeekOfMonth = WeekOfMonth.Second
    };
    SecondFridayInMonth.Add(new WorkTime(9, 12));

    // The rule applies to 31st December 5 times 
    // each two years starting from 12/31/2019.
    YearlyExceptionRule December31 = new YearlyExceptionRule() {
        DayOfMonth = 31,
        Month = Month.December,
        StartDate = new DateTime(2019, 12, 31),
        Interval = 2,
        Occurrences = 5
    };
    December31.Add(new WorkTime(9, 11));

    //The rule applies to the 256th day each year.
    YearlyDayOfYearExceptionRule Day256 = new YearlyDayOfYearExceptionRule() {
        DayOfYear = 256,
    };
    // Specify work hours for the 256th day.
    Day256.WorkTimes.Add(new WorkTime(9, 12));

    return new ExceptionRule[] { SecondFridayInMonth, FirstDayInMonth, dailyException, daysException, Day256, December31 };
}
ganttControl1.Exceptions.AddRange(CreateCustomRules());
vb
Imports DevExpress.XtraGantt
Imports DevExpress.XtraGantt.Base.Scheduling
Imports DevExpress.XtraGantt.Exceptions
Imports DevExpress.XtraGantt.Scheduling

Private Function CreateCustomRules() As ExceptionRule()

    Dim dailyException As New DailyExceptionRule() With {
        .StartDate = New Date(2019, 8, 19),
        .Occurrences = 7
    }
    dailyException.WorkTimes.Add(New WorkTime(9, 12))

    Dim daysException As New DaysExceptionRule() With {
        .StartDate = New Date(2023, 9, 27, 0, 0, 0, 0),
        .Occurrences = 1
    }

    ' The rule applies to the first day of each month (January, 1; February, 1; etc.)
    Dim FirstDayInMonth As New MonthlyExceptionRule() With {.DayOfMonth = 1}
    FirstDayInMonth.Add(New WorkTime(10, 12))

    ' The rule applies to the second Friday of each month.
    Dim SecondFridayInMonth As New MonthlyDayOfWeekExceptionRule() With {
        .DayOfWeek = DayOfWeek.Friday,
        .WeekOfMonth = WeekOfMonth.Second
    }
    SecondFridayInMonth.Add(New WorkTime(9, 12))

    ' The rule applies to 31st December 5 times 
    ' each two years starting from 12/31/2019.
    Dim December31 As New YearlyExceptionRule() With {
        .DayOfMonth = 31,
        .Month = Month.December,
        .StartDate = New Date(2019, 12, 31),
        .Interval = 2,
        .Occurrences = 5
    }
    December31.Add(New WorkTime(9, 11))

    'The rule applies to the 256th day each year.
    Dim Day256 As New YearlyDayOfYearExceptionRule() With {.DayOfYear = 256}
    ' Specify work hours for the 256th day.
    Day256.WorkTimes.Add(New WorkTime(9, 12))

    Return New ExceptionRule() { SecondFridayInMonth, FirstDayInMonth, dailyException, daysException, Day256, December31 }
End Function
ganttControl1.Exceptions.AddRange(CreateCustomRules())

The code below shows how to specify holidays.

csharp
using DevExpress.XtraGantt.Base.Scheduling;
using DevExpress.XtraGantt.Exceptions;

ganttControl1.Exceptions.AddRange(CreateExceptionRules());

ExceptionRule[] CreateExceptionRules() {
    YearlyExceptionRule NewYearDay = new YearlyExceptionRule() {
        DayOfMonth = 1,
        Month = Scheduling.Month.January
    };
    YearlyDayOfWeekExceptionRule MartinLutherDay = new YearlyDayOfWeekExceptionRule() {
        DayOfWeek = System.DayOfWeek.Monday,
        Month = Scheduling.Month.January,
        WeekOfMonth = Scheduling.WeekOfMonth.Third
    };
    YearlyDayOfWeekExceptionRule PresidentDay = new YearlyDayOfWeekExceptionRule() {
        DayOfWeek = System.DayOfWeek.Monday,
        Month = Scheduling.Month.February,
        WeekOfMonth = Scheduling.WeekOfMonth.Third
    };
    YearlyDayOfWeekExceptionRule MemorialDay = new YearlyDayOfWeekExceptionRule() {
        DayOfWeek = System.DayOfWeek.Monday,
        Month = Scheduling.Month.May,
        WeekOfMonth = Scheduling.WeekOfMonth.Last
    };
    YearlyExceptionRule IndependenceDay = new YearlyExceptionRule() {
        DayOfMonth = 4,
        Month = Scheduling.Month.July
    };
    YearlyDayOfWeekExceptionRule LaborDay = new YearlyDayOfWeekExceptionRule() {
        DayOfWeek = System.DayOfWeek.Monday,
        Month = Scheduling.Month.September,
        WeekOfMonth = Scheduling.WeekOfMonth.First
    };
    YearlyDayOfWeekExceptionRule ColumbusDay = new YearlyDayOfWeekExceptionRule() {
        DayOfWeek = System.DayOfWeek.Monday,
        Month = Scheduling.Month.October,
        WeekOfMonth = Scheduling.WeekOfMonth.Second
    };
    YearlyExceptionRule VeteransDay = new YearlyExceptionRule() {
        DayOfMonth = 11,
        Month = Scheduling.Month.November
    };
    YearlyDayOfWeekExceptionRule ThanksgivingDay = new YearlyDayOfWeekExceptionRule() {
        DayOfWeek = System.DayOfWeek.Thursday,
        Month = Scheduling.Month.November,
        WeekOfMonth = Scheduling.WeekOfMonth.Forth
    };
    YearlyExceptionRule ChristmasDay = new YearlyExceptionRule() {
        DayOfMonth = 25,
        Month = Scheduling.Month.December
    };
    return new ExceptionRule[] {
        NewYearDay,
        MartinLutherDay,
        PresidentDay,
        MemorialDay,
        IndependenceDay,
        LaborDay,
        ColumbusDay,
        VeteransDay,
        ThanksgivingDay,
        ChristmasDay
    };
}
vb
Imports DevExpress.XtraGantt.Base.Scheduling
Imports DevExpress.XtraGantt.Exceptions

ganttControl1.Exceptions.AddRange(CreateExceptionRules())

Private Function CreateExceptionRules() As ExceptionRule()
    Dim NewYearDay As New YearlyExceptionRule() With {.DayOfMonth = 1, .Month = Scheduling.Month.January}
    Dim MartinLutherDay As New YearlyDayOfWeekExceptionRule() With {.DayOfWeek = System.DayOfWeek.Monday, .Month = Scheduling.Month.January, .WeekOfMonth = Scheduling.WeekOfMonth.Third}
    Dim PresidentDay As New YearlyDayOfWeekExceptionRule() With {.DayOfWeek = System.DayOfWeek.Monday, .Month = Scheduling.Month.February, .WeekOfMonth = Scheduling.WeekOfMonth.Third}
    Dim MemorialDay As New YearlyDayOfWeekExceptionRule() With {.DayOfWeek = System.DayOfWeek.Monday, .Month = Scheduling.Month.May, .WeekOfMonth = Scheduling.WeekOfMonth.Last}
    Dim IndependenceDay As New YearlyExceptionRule() With {.DayOfMonth = 4, .Month = Scheduling.Month.July}
    Dim LaborDay As New YearlyDayOfWeekExceptionRule() With {.DayOfWeek = System.DayOfWeek.Monday, .Month = Scheduling.Month.September, .WeekOfMonth = Scheduling.WeekOfMonth.First}
    Dim ColumbusDay As New YearlyDayOfWeekExceptionRule() With {.DayOfWeek = System.DayOfWeek.Monday, .Month = Scheduling.Month.October, .WeekOfMonth = Scheduling.WeekOfMonth.Second}
    Dim VeteransDay As New YearlyExceptionRule() With {.DayOfMonth = 11, .Month = Scheduling.Month.November}
    Dim ThanksgivingDay As New YearlyDayOfWeekExceptionRule() With {.DayOfWeek = System.DayOfWeek.Thursday, .Month = Scheduling.Month.November, .WeekOfMonth = Scheduling.WeekOfMonth.Forth}
    Dim ChristmasDay As New YearlyExceptionRule() With {.DayOfMonth = 25, .Month = Scheduling.Month.December}
    Return New ExceptionRule() { NewYearDay, MartinLutherDay, PresidentDay, MemorialDay, IndependenceDay, LaborDay, ColumbusDay, VeteransDay, ThanksgivingDay, ChristmasDay }
End Function

Note

Run the XtraGantt demo to see the complete example. Click Open Solution in the ribbon for source codes.