This example illustrates how to handle the SchedulerStorageBase.FetchAppointments Event when the Scheduler is bound to the Entity Framework datasource.
As we mentioned in the How to implement FetchAppointments event handling when binding to a collection of custom objects example, you should not modify the SchedulerStorage.Appointments or SchedulerStorage.Appointments.DataSource property directly in the FetchAppointments event handler. Instead, the bound datasource should be filtered according to the currently visible time interval. Since the Entity Framework does not provide a convenient method for implementing this logic, we decided to use an intermediate BindingSource Class. Initially, we assign it to the SchedulerStorage.Appointments.DataSource property. In the FetchAppointments event handler we modify the datasource of this intermediate object. Here is the corresponding code snippet:
[C#]privatevoidStorage_FetchAppointments(objectsender,FetchAppointmentsEventArgse){BindingSourcebindingSource=schedulerControl1.Storage.Appointments.DataSourceasBindingSource;if(bindingSource==null||lastFetchedInterval.Contains(e.Interval))return;TimeSpanmargin=TimeSpan.FromDays(0);// TimeSpan.FromDays(1) lastFetchedInterval=newTimeInterval(e.Interval.Start-margin,e.Interval.End+margin);varentities=fromentityincarsXtraSchedulingEntities.CarSchedulingswhereentity.EventType== 1 ||(entity.StartTime<lastFetchedInterval.End&&entity.EndTime>lastFetchedInterval.Start)selectentity;bindingSource.DataSource=entities;}
[VB.NET]PrivateSub Storage_FetchAppointments(ByVal sender AsObject, ByVal e As FetchAppointmentsEventArgs) Handles schedulerStorage1.FetchAppointmentsDim bindingSource As BindingSource = TryCast(schedulerControl1.Storage.Appointments.DataSource, BindingSource)If bindingSource IsNothing OrElse lastFetchedInterval.Contains(e.Interval) ThenReturnEndIfDim margin As TimeSpan = TimeSpan.FromDays(0) ' TimeSpan.FromDays(1) lastFetchedInterval = New TimeInterval(e.Interval.Start - margin, e.Interval.End + margin)Dim entities = _From entity In carsXtraSchedulingEntities.CarSchedulings _Where entity.EventType = 1 OrElse (entity.StartTime < lastFetchedInterval.EndAndAlso entity.EndTime > lastFetchedInterval.Start) _Select entity bindingSource.DataSource = entities EndSub
We have created a data model in this example by using the "Database First" EF approach. You can check the Create the First simple Entity Data Model and related web articles to learn more on this subject.
To test this example locally, set up the CarsXtraScheduling sample database in your SQL Server instance. This database script is included along with the attached solution.
See Also:
How to bind SchedulerControl to Entity Framework by using Entity Data Model
Question Comments
Added By: SamDev at: 7/18/2016 11:46:19 AM
where entity.EventType == 1 || (entity.StartTime < lastFetchedInterval.End && entity.EndTime > lastFetchedInterval.Start)
For anyone following this example the above code does not work when changed appointments are moved out of the range your looking at. You need to load not just all patterns but changed appointments. In another post I warn about this issue. The changed appointments if within the range being viewed will show up properly. If out of the range it will seem as is you never have a changed appointment at all !!