New to Telerik UI for WinFormsStart a free 30-day trial

Scheduler Mapping

Updated over 6 months ago

The SchedulerMapping class is responsible for mapping a single property from the data source to a scheduler property, which allows you to specify convert callback methods in order to convert values to and from the data source if needed.

Overview

To associate a field with a different type than the scheduler type, for example, a background id that is written as a string in the database:

1. Create two methods with the following signatures:

C#
object ConvertBackgrounIdToScheduler(object obj)
{
    if (!(obj is DBNull))
    {
        string appointmentBackground = (string)obj;
        switch (appointmentBackground)
        {
            case "Business":
                return AppointmentBackground.Business;
            case "Important":
                return AppointmentBackground.Important;
            case "MustAttend":
                return AppointmentBackground.MustAttend;
            case "PhoneCall":
                return AppointmentBackground.PhoneCall;
            //TO DO other backgrounds
        }
    }
    return AppointmentBackground.None;
}
object ConvertBackgrounIdToDataSource(object obj)
{
    AppointmentBackground appointmentBackground = (AppointmentBackground)obj;
    switch (appointmentBackground)
    {
        case AppointmentBackground.Business:
            return "Business";
        case AppointmentBackground.Important:
            return "Important";
        case AppointmentBackground.MustAttend:
            return "MustAttend";
        case AppointmentBackground.PhoneCall:
            return "PhoneCall";
        //TO DO other backgrounds
    }
    return "None";
}

2. Apply settings of the BackgroundId mapping in the AppointmentMappingInfo:

C#
SchedulerMapping backgroundIdSchedulerMapping = appointmentMappingInfo.FindBySchedulerProperty("BackgroundId");
backgroundIdSchedulerMapping.ConvertToScheduler = new ConvertCallback(this.ConvertBackgrounIdToScheduler);
backgroundIdSchedulerMapping.ConvertToDataSource = new ConvertCallback(this.ConvertBackgrounIdToDataSource);

Null Fields in the Data Base

If the database has fields that can be Null then you need to add converters for those fields. For example, let's suppose that the database has a field "Location", which is a string and you wish to check whether its value is DBNull. If it is DBNull you would want to convert it to null. Here are the steps:

1. Create a conversion method with the following signature:

C#
object ConvertLocationToScheduler(object obj)
{
    if (obj is DBNull)
    {
        return null;
    }
    return obj;
}

2. Apply the Location mapping settings in the AppointmentMappingInfo:

C#
AppointmentMappingInfo appointmentMappingInfo = new AppointmentMappingInfo();
appointmentMappingInfo.Location = "Location";
SchedulerMapping locationSchedulerMapping = appointmentMappingInfo.FindBySchedulerProperty("Location");
locationSchedulerMapping.ConvertToScheduler = new ConvertCallback(this.ConvertLocationToScheduler);

Not Read-Only Id Field Other than Guid

If the Id field type is different than a Guid, and/or it is not read only in the data table, then you will need to add a converter to convert the type to a Guid. This case holds true for Resources and Appointments.

If Id is read only in the data set, then the Scheduler will rely on the way the data set handles read only Id's.

1. Create a methods with the following signatures

C#
object ConvertUniqueIdToScheduler(object obj)
{
    return new EventId(obj);
}
object ConvertUniqueIdToDataSource(object obj)
{
    EventId eventId = obj as EventId;
    //if the appointment is added from the scheduler the eventId.KeyValue is Guid
    if (eventId.KeyValue is Guid)
    {
        //return the value that is in your object/dataset
    }
    //if the appointment is loaded from an object/dataset the eventId.KeyValue is its representation in the object/datase
    return eventId.KeyValue;
}

2. Apply the settings of the UniqueId mapping in the AppointmentMappingInfo:

C#
SchedulerMapping uniqueIdSchedulerMapping = appointmentMappingInfo.FindBySchedulerProperty("UniqueId");
uniqueIdSchedulerMapping.ConvertToScheduler = new ConvertCallback(this.ConvertUniqueIdToScheduler);
uniqueIdSchedulerMapping.ConvertToDataSource = new ConvertCallback(this.ConvertUniqueIdToDataSource);

ConvertValueToScheduler and ConvertValueToDataSource Events

Similar to the ConvertToScheduler and ConvertToDataSource callbacks of the SchedulerMapping, in R1 2018 we introduced the ConvertValueToScheduler and ConvertValueToDataSource events to handle the conversion from a value coming from the DataSource to the RadScheduler's respective type and vice versa.

Conversion Events

C#
public void EventSubscriptions()
{
    AppointmentMappingInfo appointmentMapping = new AppointmentMappingInfo();
    //other code for the AppointmentMappingInfo setup
    appointmentMapping.FindBySchedulerProperty("ResourceId").ConvertValueToScheduler += ConvertValueToScheduler;
    appointmentMapping.FindBySchedulerProperty("ResourceId").ConvertValueToDataSource += ConvertValueToDataSource;
}
private void ConvertValueToDataSource(object sender, ConvertFromToSchedulerEventArgs e)
{
    if (e.PropertyName == "ResourceId")
    {
        e.Value = (int)((EventId)e.Value).KeyValue;
    }
}
private void ConvertValueToScheduler(object sender, ConvertFromToSchedulerEventArgs e)
{
    if (e.PropertyName == "ResourceId")
    {
        e.Value = new EventId((int)e.Value);
    }
}

See Also