Back to Devexpress

How to: Bind a Scheduler Storage to a RangeControl

windowsforms-11896-controls-and-libraries-scheduler-examples-rangecontrol-how-to-bind-a-scheduler-storage-to-a-rangecontrol.md

latest11.3 KB
Original Source

How to: Bind a Scheduler Storage to a RangeControl

  • Nov 13, 2018
  • 5 minutes to read

This example demonstrates how to bind the SchedulerStorage to RangeControl so that the appointment data held within the scheduler storage is rendered in the RangeControl’s viewport. The GridControl control is used to show detailed information on appointments (appointment’s subject, start time, end time and description) that belong to the time interval that is currently selected in the RangeControl.

  1. Create an instance of the SchedulerStorageRangeControlClient class with the specified SchedulerStorage object. This class implements the IRangeControlClient interface and allows you to create a Range Control Client that will visualize the appointment data contained within the specified SchedulerDataStorage in the RangeControl viewport.
  2. Assign the created SchedulerStorageRangeControlClient object to the RangeControl.Client property to bind the SchedulerStorage to the RangeContorl.
  3. Use the RangeControl.SelectedRange property to set the time range that will be initially selected in the RangeControl after an application has been invoked.
  4. Subscribe to RangeControl.RangeChanged event. In this event handler, call the SchedulerStorageBase.GetAppointments method to retrieve a collection of appointments contained within the time interval that is currently selected in the RangeControl and assign this collection to the GridControl.DataSource property to display corresponding appointments in the Grid Control.
csharp
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SchedulerStorageRangeControl {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            //DevExpress.UserSkins.BonusSkins.Register();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
csharp
using System;
using System.Windows.Forms;
using DevExpress.XtraScheduler;
using DevExpress.XtraEditors;

namespace SchedulerStorageRangeControl {
    public partial class Form1 : Form {
        public static Random RandomInstance = new Random();

        public Form1() {
            InitializeComponent();
            // Create an object specifying the scheduler storage as a client of the RangeControl
            // to visualize appointment data within the RangeControl's viewport.
            Client = new SchedulerStorageRangeControlClient(schedulerStorage1);
            Client.BeginInit();
            Client.Options.RangeMinimum = DateTime.Today.AddMonths(-1);
            Client.Options.RangeMaximum = DateTime.Today.AddMonths(1);
            Client.EndInit();
            // Bind the scheduler storage to the RangeControl.
            rangeControl.Client = Client;

            // Fill the scheduler storage with appointment data.
            FillStorageData();

            // Set the time range that will be initially selected within the RangeControl.
            rangeControl.SelectedRange = 
                                    new RangeControlRange(DateTime.Today, DateTime.Today.AddDays(1));
        }
        public SchedulerStorageRangeControlClient Client { get; private set; }

        private void FillStorageData() {
            DateTime date = Client.Options.RangeMinimum;
            TimeSpan range = Client.Options.RangeMaximum - Client.Options.RangeMinimum;
            int count = range.Days;
            for (int i = 0; i < count; i++) {
                int appointmentsPerDay = RandomInstance.Next(15);
                GenerateDayAppointments(date, appointmentsPerDay);
                date = date.AddDays(1);
            }
        }
        protected void GenerateDayAppointments(DateTime date, int count) { 
            for (int i = 0; i < count; i++) {
                string subject = string.Format("Task #{0}", i + 1);
                DateTime start = date.AddHours(RandomInstance.Next(16));
                DateTime end = start.AddHours(RandomInstance.Next(3));
                int labelId = RandomInstance.Next(11);

                CreateAppointment(start, end, subject, labelId);
            }
        }
        private void CreateAppointment(DateTime start, DateTime end, string subject, int labelId) {
            Appointment apt = schedulerStorage1.CreateAppointment(AppointmentType.Normal);
            apt.Start = start;
            apt.End = end;
            apt.Subject = subject;
            apt.LabelKey = labelId;
            apt.Description = string.Format("Event time from {0} to {1}", 
                                            start.ToShortTimeString(), end.ToShortTimeString());
            schedulerStorage1.Appointments.Add(apt);
        }

        // Make the GridControl display appointments contained in the time range
        // that is currently selected within the RangeControl.
        private void RangeControlRangeChanged(object sender, RangeControlRangeEventArgs e) {
            DateTime start = Convert.ToDateTime(e.Range.Minimum);
            DateTime end = Convert.ToDateTime(e.Range.Maximum);

            AppointmentBaseCollection appointments = schedulerStorage1.GetAppointments(start, end);

            int count = appointments.Count;
            Text = String.Format("[{0} - {1}] Count={2}", start, end, count);

            gridControl1.DataSource = appointments;
        }

    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms

Namespace SchedulerStorageRangeControl
    Friend NotInheritable Class Program

        Private Sub New()
        End Sub

        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread> _
        Shared Sub Main()
            'DevExpress.UserSkins.BonusSkins.Register();

            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraEditors

Namespace SchedulerStorageRangeControl
    Partial Public Class Form1
        Inherits Form

        Public Shared RandomInstance As New Random()

        Public Sub New()
            InitializeComponent()
            ' Create an object specifying the scheduler storage as a client of the RangeControl
            ' to visualize appointment data within the RangeControl's viewport.
            Client = New SchedulerStorageRangeControlClient(schedulerStorage1)
            Client.BeginInit()
            Client.Options.RangeMinimum = Date.Today.AddMonths(-1)
            Client.Options.RangeMaximum = Date.Today.AddMonths(1)
            Client.EndInit()
            ' Bind the scheduler storage to the RangeControl.
            rangeControl.Client = Client

            ' Fill the scheduler storage with appointment data.
            FillStorageData()

            ' Set the time range that will be initially selected within the RangeControl.
            rangeControl.SelectedRange = New RangeControlRange(Date.Today, Date.Today.AddDays(1))
        End Sub
        Private privateClient As SchedulerStorageRangeControlClient
        Public Property Client() As SchedulerStorageRangeControlClient
            Get
                Return privateClient
            End Get
            Private Set(ByVal value As SchedulerStorageRangeControlClient)
                privateClient = value
            End Set
        End Property

        Private Sub FillStorageData()
            Dim [date] As Date = Client.Options.RangeMinimum
            Dim range As TimeSpan = Client.Options.RangeMaximum - Client.Options.RangeMinimum
            Dim count As Integer = range.Days
            For i As Integer = 0 To count - 1
                Dim appointmentsPerDay As Integer = RandomInstance.Next(15)
                GenerateDayAppointments([date], appointmentsPerDay)
                [date] = [date].AddDays(1)
            Next i
        End Sub
        Protected Sub GenerateDayAppointments(ByVal [date] As Date, ByVal count As Integer)
            For i As Integer = 0 To count - 1
                Dim subject As String = String.Format("Task #{0}", i + 1)
                Dim start As Date = [date].AddHours(RandomInstance.Next(16))
                Dim [end] As Date = start.AddHours(RandomInstance.Next(3))
                Dim labelId As Integer = RandomInstance.Next(11)

                CreateAppointment(start, [end], subject, labelId)
            Next i
        End Sub
        Private Sub CreateAppointment(ByVal start As Date, ByVal [end] As Date, ByVal subject As String, ByVal labelId As Integer)
            Dim apt As Appointment = schedulerStorage1.CreateAppointment(AppointmentType.Normal)
            apt.Start = start
            apt.End = [end]
            apt.Subject = subject
            apt.LabelKey = labelId
            apt.Description = String.Format("Event time from {0} to {1}", start.ToShortTimeString(), [end].ToShortTimeString())
            schedulerStorage1.Appointments.Add(apt)
        End Sub

        ' Make the GridControl display appointments contained in the time range
        ' that is currently selected within the RangeControl.
        Private Sub RangeControlRangeChanged(ByVal sender As Object, ByVal e As RangeControlRangeEventArgs) Handles rangeControl.RangeChanged
            Dim start As Date = Convert.ToDateTime(e.Range.Minimum)
            Dim [end] As Date = Convert.ToDateTime(e.Range.Maximum)

            Dim appointments As AppointmentBaseCollection = schedulerStorage1.GetAppointments(start, [end])

            Dim count As Integer = appointments.Count
            Text = String.Format("[{0} - {1}] Count={2}", start, [end], count)

            gridControl1.DataSource = appointments
        End Sub

    End Class
End Namespace

The image below shows the result.

See Also

How to: Use RangeControl in a Scheduling Application