Friday, September 04, 2009
by
Evan Hutnick
|
If you've been along for the ride so far with this series, you already know that we've hooked a SQL database up to RIA Services using an ADO.Net Entity Data Model and a DomainDataService data context. If you've missed it, check out the prior two posts (or search RadScheduler for Silverlight learning series in the Telerik Blogs site). Now that we're here, we have three more events to handle and we've got a fully-functional RadScheduler for Silverlight hooked up to RIA services.
First up, the two easy events- AppointmentAdded and AppointmentDeleted.
AppointmentAdded Event
This event is pretty straightforward and does not require very much explanation. When it fires, RadScheduler has already created an appointment internally that comes to us in the argument e.Appointment. Once we have this, we simply turn the appointment into an SqlAppointment (which our data context understands), generate a recurrence string from the recurrence pattern of the appointment (don't gotta worry about exceptions because a new appointment won't have exceptions), and add it to our data context. You can do this as follows:
| private void xRadScheduler_AppointmentAdded(object sender, Telerik.Windows.Controls.AppointmentAddedEventArgs e) |
| { |
| Appointment app = e.Appointment as Appointment; |
| |
| SqlAppointments sq = new SqlAppointments(); |
| sq.Start = app.Start; |
| sq.End = app.End; |
| sq.Subject = app.Subject; |
| sq.Body = app.Body; |
| sq.IsAllDayEvent = app.IsAllDayEvent; |
| sq.Location = app.Location; |
| sq.Url = app.Url; |
| sq.UniqueId = new Guid(app.UniqueId); |
| sq.Type = (int)AppointmentType.Regular; |
| |
| if (app.RecurrenceRule != null) |
| { |
| sq.RecurrencePattern = RecurrencePatternHelper.RecurrencePatternToString(app.RecurrenceRule.Pattern); |
| } |
| |
| var context = DDS.DomainContext as SchedulerContext; |
| context.SqlAppointments.Add(sq); |
| context.SubmitChanges(delegate { RadWindow.Alert("Added!"); }, null); |
| } |
AppointmentDeleted Event
Again, it is pretty wasy to figure out what is happening on this one. RadScheduler has already deleted the appointment internally, so all we need to do is to let the context know which appointment we are deleting. Since all recurrence exception info is stored in our appointments we don't need any complex operations, so this next part is pretty self-explanatory:
| private void xRadScheduler_AppointmentDeleted(object sender, Telerik.Windows.Controls.AppointmentDeletedEventArgs e) |
| { |
| // Grab our SchedulerContext and our appointment that was deleted |
| var context = DDS.DomainContext as SchedulerContext; |
| Appointment anApp = e.Appointment as Appointment; |
| |
| // We want to remove the appointment from the context based on UniqueID |
| context.SqlAppointments.Remove(context.SqlAppointments.Single(x => x.UniqueId == new Guid(anApp.UniqueId))); |
| context.SubmitChanges(delegate { RadWindow.Alert("Removed!"); }, anApp); |
| } |
| |
AppointmentEdited Event
Now we get to the fun stuff. :)