How to: Handle Appointment Conflicts
- 5 minutes to read
This example demonstrates how to manually determine whether there are appointment conflicts or not in some particular situations. If you’re not satisfied with the automatic conflicts determination of the XtraScheduler (when appointments are considered to conflict if they have the same resource and their time intervals intersect), you can set the SchedulerOptionsCustomization.AllowAppointmentConflicts property to Custom and handle the SchedulerControl.AllowAppointmentConflicts event to perform your own conflict determination.
In the following example it’s assumed that an appointment is a lecture conducted by a teacher in a classroom, and several groups of students may be present at the same lecture at the same time. Two appointments are consider to be in conflict in the following situations.
- Two different lecturers are scheduled to conduct a lecture at the same time in the same room.
- The same lecturer is scheduled to conduct different lectures at the same time in different rooms.
- The same group is scheduled at the same time in different rooms.
using DevExpress.XtraScheduler;
// ...
// Appointment = Lecture
// Resource = Room
// appointment.CustomFields["Teacher"] = Teacher
// appointment.CustomFields["Group"] = Group of Students
// Start of the AllowAppointmentConflicts event handler.
// ==================================================
private void schedulerControl1_AllowAppointmentConflicts(object sender,
AppointmentConflictEventArgs e) {
Appointment currentLecture = e.Appointment;
object roomId = currentLecture.ResourceId;
string teacher = GetTeacher(currentLecture);
string group = GetGroup(currentLecture);
DateTime start = currentLecture.Start;
TimeSpan duration = currentLecture.Duration;
// e.Conflicts contains all the lectures held at the same time.
// All of them the SchedulerControl considers to be conflicting.
AppointmentBaseCollection lecturesInTheSameTime = e.Conflicts;
AppointmentBaseCollection conflictedLectures = new AppointmentBaseCollection();
AppointmentBaseCollection lecturesInDifferentRoomWithDifferentTeacher =
new AppointmentBaseCollection();
ArrayList groupsInDifferentRoomWithDifferentTeacher = new ArrayList();
ArrayList groups = new ArrayList();
int count = lecturesInTheSameTime.Count;
for (int i = 0; i < count; i++) {
Appointment lecture = lecturesInTheSameTime[i];
// Check if the lecture is in the same room.
if (Object.Equals(lecture.ResourceId, roomId)) {
// Check if the lecture is by the same teacher.
if (String.Compare(teacher, GetTeacher(lecture), true) == 0) {
if (lecture.Start != start || lecture.Duration != duration) {
// Conflict! Lecture with a bad time frame.
conflictedLectures.Add(lecture);
}
else {
// No conflict!
groups.Add(GetGroup(lecture));
}
}
// Lecture by a different teacher.
else {
// Conflict! Lecture by a different teacher in the same room.
conflictedLectures.Add(lecture);
}
}
// Lecture in a different room.
else {
// Lecture by the same teacher.
if (String.Compare(teacher, GetTeacher(lecture), true) == 0) {
// Conflict! Lecture of the same teacher in the different room.
conflictedLectures.Add(lecture);
}
// Lecture by a different teacher.
else {
// No conflict! Lecture by a different teacher in a different room.
groupsInDifferentRoomWithDifferentTeacher.Add(GetGroup(lecture));
lecturesInDifferentRoomWithDifferentTeacher.Add(lecture);
}
}
}
// Search for the groups which should be in different rooms at the same time.
count = groups.Count;
for (int i = 0; i < count; i++) {
int conflictIndex = groupsInDifferentRoomWithDifferentTeacher.IndexOf(groups[i]);
if (conflictIndex >= 0)
conflictedLectures.Add(lecturesInDifferentRoomWithDifferentTeacher[conflictIndex]);
}
e.Conflicts.Clear();
// If the conflictedLectures will contain no lectures after this event has occured,
// then e.Conflicts will be empty and this will indicate that there are no conflicts.
e.Conflicts.AddRange(conflictedLectures);
}
// End of the AllowAppointmentConflicts event handler.
// ==================================================
// Additional functions.
// ====================
// Determines the teacher for the specified lecture.
string GetTeacher(Appointment lecture) {
object teacher = lecture.CustomFields["Teacher"];
if (teacher == null)
return String.Empty;
return teacher.ToString();
}
// Determines the group for the specified lecture.
string GetGroup(Appointment lecture) {
object group = lecture.CustomFields["Group"];
if (group == null)
return String.Empty;
return group.ToString();
}