This example shows how to create a comparer to sort Scheduler resources based on a specific condition (in this example, the Resource.Caption
and the number of resource appointments).
The example also demonstrates how to prevent sorting ("ResourceNOfAppointments" mode) for an "Unassigned" resource. This resource is always displayed first.
The SchedulerStorageBase.ResourceCollectionLoaded event is handled to automaticcally sort resources when the underlying collection changes:
C#private void schedulerStorage1_ResourceCollectionLoaded(object sender, System.EventArgs e) {
ApplySorting();
}
private void ApplySorting() {
IComparer<Resource> comparer = null;
if (CurrentSortOrder == ResourcesSortOrder.Ascending)
comparer = new ResourceCaptionComparer();
else if (CurrentSortOrder == ResourcesSortOrder.Descending)
comparer = new ResourceCaptionReverseComparer();
else if (CurrentSortOrder == ResourcesSortOrder.NOfAppointments)
comparer = new ResourceNOfAppointmentsComparer(schedulerStorage1);
else
return;
schedulerStorage1.Resources.Items.Sort(comparer);
schedulerControl1.ActiveView.LayoutChanged();
}
Files to Review
- Comparers.cs (VB: Comparers.vb)
- Form1.cs (VB: Form1.vb)
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Collections;
using System.Collections.Generic;
using DevExpress.XtraScheduler;
namespace SchedulerSortResources {
public abstract class ResourceBaseComparer : IComparer<Resource>, IComparer {
#region IComparer Members
int IComparer.Compare(object x, object y) {
return CompareCore(x, y);
}
public int Compare(Resource x, Resource y) {
return CompareCore(x, y);
}
#endregion
protected virtual int CompareCore(object x, object y) {
Resource xRes = (Resource)x;
Resource yRes = (Resource)y;
if (xRes == null || yRes == null)
return 0;
if (ResourceEmpty.Resource.Equals(xRes) || ResourceEmpty.Resource.Equals(yRes))
return 0;
return CompareResources(xRes, yRes);
}
protected abstract int CompareResources(Resource xRes, Resource yRes);
}
public class ResourceCaptionComparer : ResourceBaseComparer {
protected override int CompareResources(Resource xRes, Resource yRes) {
return String.Compare(xRes.Caption, yRes.Caption);
}
}
public class ResourceCaptionReverseComparer : ResourceBaseComparer {
protected override int CompareResources(Resource xRes, Resource yRes) {
return String.Compare(yRes.Caption, xRes.Caption);
}
}
public class ResourceNOfAppointmentsComparer : ResourceBaseComparer {
private SchedulerStorage schedulerStorage;
public ResourceNOfAppointmentsComparer(SchedulerStorage schedulerStorage) {
this.schedulerStorage = schedulerStorage;
}
protected override int CompareResources(Resource xRes, Resource yRes) {
// "Unassigned" resource
if (Convert.ToInt32(yRes.Id) == -1)
return 1;
// "Unassigned" resource
if (Convert.ToInt32(xRes.Id) == -1)
return -1;
int order = schedulerStorage.Appointments.Items.FindAll(e => e.ResourceId.Equals(yRes.Id)).Count -
schedulerStorage.Appointments.Items.FindAll(e => e.ResourceId.Equals(xRes.Id)).Count;
if (order == 0)
return schedulerStorage.Resources.Items.IndexOf(xRes) - schedulerStorage.Resources.Items.IndexOf(yRes);
return order;
}
}
}
C#using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraScheduler;
using System.Collections.Generic;
namespace SchedulerSortResources {
public partial class Form1 : Form {
private DataModel dataModel = new DataModel();
public ResourcesSortOrder CurrentSortOrder {
get {
return (ResourcesSortOrder)comboBox1.SelectedIndex;
}
set {
comboBox1.SelectedIndex = (int)value;
}
}
public Form1() {
InitializeComponent();
FillResources();
CurrentSortOrder = ResourcesSortOrder.Ascending;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
if (CurrentSortOrder == ResourcesSortOrder.None)
schedulerControl1.RefreshData();
else
ApplySorting();
}
private void schedulerStorage1_ResourceCollectionLoaded(object sender, System.EventArgs e) {
ApplySorting();
}
private void AppointmentsModified(object sender, PersistentObjectsEventArgs e) {
ApplySorting();
}
private void ApplySorting() {
IComparer<Resource> comparer = null;
if (CurrentSortOrder == ResourcesSortOrder.Ascending)
comparer = new ResourceCaptionComparer();
else if (CurrentSortOrder == ResourcesSortOrder.Descending)
comparer = new ResourceCaptionReverseComparer();
else if (CurrentSortOrder == ResourcesSortOrder.NOfAppointments)
comparer = new ResourceNOfAppointmentsComparer(schedulerStorage1);
else
return;
schedulerStorage1.Resources.Items.Sort(comparer);
schedulerControl1.ActiveView.LayoutChanged();
}
private void FillResources() {
string[] users = new string[] { "Peter Dolan", "Ryan Fischer", "Andrew Miller", "Tom Hamlett",
"Jerry Campbell", "Carl Lucas" };
dataModel.Resources.AddResourcesRow("Unassigned", Color.DarkGray.ToArgb());
for (int i = 0; i < users.Length; i++) {
dataModel.Resources.AddResourcesRow(users[i], schedulerControl1.ResourceColorSchemas[i].Cell.ToArgb());
}
schedulerStorage1.Resources.ColorSaving = ColorSavingType.ArgbColor;
schedulerStorage1.Resources.DataSource = dataModel;
}
}
public enum ResourcesSortOrder {
None = 0,
Ascending = 1,
Descending = 2,
NOfAppointments = 3
}
}