How to: Implement Dependency Constraints in the Gantt View

  • 2 minutes to read

This code snippet illustrates how to apply the AppointmentDependencyType.FinishToStart dependency restrictions on modifications of appointment Appointment.Start and Appointment.End values within the Gantt View.

The SchedulerOptionsCustomization.AllowAppointmentConflicts option of the Scheduler is set to AppointmentConflictsMode.Custom. It allows the SchedulerControl to raise the SchedulerControl.AllowAppointmentConflicts event.

The code within this event handler analyzes all dependencies of the specified (Finish-to-Start) type established with the appointment for which the event is raised. If Start or End properties of the modified appointment do not meet criteria, it is thought that a conflict occurs.

If a conflict occurs, appointment modifications are canceled automatically.

private void schedulerControl1_AllowAppointmentConflicts(object sender, AppointmentConflictEventArgs e)
{
    e.Conflicts.Clear();

    AppointmentDependencyBaseCollection depCollectionDep = 
        schedulerStorage1.AppointmentDependencies.Items.GetDependenciesByDependentId(e.Appointment.Id);
    if (depCollectionDep.Count > 0) {
        if (CheckForInvalidDependenciesAsDependent(depCollectionDep, e.AppointmentClone))
            e.Conflicts.Add(e.AppointmentClone);
    }

    AppointmentDependencyBaseCollection depCollectionPar = 
        schedulerStorage1.AppointmentDependencies.Items.GetDependenciesByParentId(e.Appointment.Id);
    if (depCollectionPar.Count > 0) {
        if (CheckForInvalidDependenciesAsParent(depCollectionPar, e.AppointmentClone))
            e.Conflicts.Add(e.AppointmentClone);
    }
}

private bool CheckForInvalidDependenciesAsDependent(AppointmentDependencyBaseCollection depCollection, Appointment apt)
{
    foreach (AppointmentDependency dep in depCollection) {
        if (dep.Type == AppointmentDependencyType.FinishToStart) {
            DateTime checkTime = schedulerStorage1.Appointments.Items.GetAppointmentById(dep.ParentId).End;
            if (apt.Start < checkTime)
                return true;
        }
    }
    return false;
}

private bool CheckForInvalidDependenciesAsParent(AppointmentDependencyBaseCollection depCollection, Appointment apt)
{
    foreach (AppointmentDependency dep in depCollection) {
        if (dep.Type == AppointmentDependencyType.FinishToStart) {
            DateTime checkTime = schedulerStorage1.Appointments.Items.GetAppointmentById(dep.DependentId).Start;
            if (apt.End > checkTime)
                return true;
        }
    }
    return false;
}