This is a migrated thread and some comments may be shown as answers.

Looping through appointments by date range

4 Answers 378 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Adrian Barnes
Top achievements
Rank 1
Adrian Barnes asked on 28 Feb 2011, 11:21 PM
Outside of the scheduler, I'm trying to loop through all events in a given date range.  Due to the complexity of recurrence, I have a hidden instance of the RadScheduler on my form, and I am looping through the appointments and resources examining each appointment to see if it falls within range.  The process is not smooth.  I'm having lots of trouble with recurrence.  For example, if I select one date, I'm getting two copies of the same event...one with, say, an ID of 105, and the other with an ID of 105_0.  If I select a week, I am only seeing the 105_0 appointment.

I am finding work-arounds for each issue as I encounter one, but do not have faith that there aren't other hidden problems. Is there a better way to loop through by date range?  The code below is just my latest experimentation. I probably need to scrap and start over.

private void LoopEvents()
{
    RadListBox rlbResources = PanelBar.Items[0].Items[0].FindControl("rlbResources") as RadListBox;
    DataTable dt;
    string canceled = "Canceled";
 
    string sql = "SELECT * FROM AppointmentResource";
    string errString = "";
    DateTime currdt = Convert.ToDateTime("1/1/1901");
 
    dt = PageData.GetData(sql, ref errString).Tables[0];
    if (rlbResources.Items.Count == 0)
        rlbResources.DataBind();
 
    foreach (Telerik.Web.UI.RadDate rd in RadCalendar1.SelectedDates)
    {
        foreach (Telerik.Web.UI.Appointment a in RadScheduler1.Appointments)
        {
                foreach (Telerik.Web.UI.RadListBoxItem l in rlbResources.Items)
                {
                    if (l.Checked & a.Resources.GetResource("Resource", Convert.ToInt32(l.Value)) != null)
                    {
                         
                        //Occurrences of a recurrence master are not generated untill they
                        //are within RadScheduler's visible range. This mandates the occurrences
                        //information to be extracted from the Master's recurrence rule as follows:
                        if ((a.RecurrenceState == RecurrenceState.Master) & RadCalendar1.SelectedDates.Count > 1)
                        {
                            RecurrenceRule parsedRule;
                            RecurrenceRule.TryParse(a.RecurrenceRule.ToString(), out parsedRule);
 
                            //If a recurring appointment does not have specified end time it will have
                            //endless occurrences. In this case you can explicitly put a restriction:
 
                            if (RadCalendar1.SelectedDates.Count > 1)
                                parsedRule.SetEffectiveRange(RadCalendar1.SelectedDates[0].Date, RadCalendar1.SelectedDates[RadCalendar1.SelectedDates.Count - 1].Date);
                            else
                                parsedRule.SetEffectiveRange(RadCalendar1.SelectedDate.AddDays(-1), RadCalendar1.SelectedDate.AddDays(1));
                             
                            foreach (DateTime occurrence in parsedRule.Occurrences)
                            {
                                if (occurrence.ToShortDateString() == rd.Date.ToShortDateString())
                                {
                                        //We have a match
                                }
                            }
                        }
                        else //Get the rest of the appointments    
 
                            //If the SelectedDate of RadScheduler is set so that the visible range encompasses occurences of     
                            //a recurring appointement, these occurences will be generated now so they will be added once again    
                            //to the DataTable. To prevent this, we use the following check:    
                            //if (a.RecurrenceState != RecurrenceState.Occurrence)
                            //{
                            if (WithinDate(a.Start, a.End, rd.Date))
                            {
                                  //We have a match
                            }
                        //}
 
                    }
                }
        }
    }
 
}

4 Answers, 1 is accepted

Sort by
0
André
Top achievements
Rank 1
answered on 01 Mar 2011, 01:44 PM

You can LINQ them to get the range you want. 

For example:

int daysRange;
switch(RadSchedulerRoteiros.SelectedView)
{
    case SchedulerViewType.DayView:
        daysRange = 1;
        break;
  
    case SchedulerViewType.WeekView:
        daysRange = 7;
        break;
      
    case SchedulerViewType.MonthView:
        daysRange = 30;
        break;
  
    default:
        break;
}
  
foreach (Appointment appointment in RadSchedulerRoteiros.Appointments.Where(A => A.Start >= RadSchedulerRoteiros.SelectedDate && A.End <= RadSchedulerRoteiros.SelectedDate.AddDays(daysRange).AddHours(23).AddMinutes(59)))
{
    //do stuff
}

This is just an example, and is not getting TimelineView and MultiDayView, as I don't know how to get the range for them... 

0
Adrian Barnes
Top achievements
Rank 1
answered on 01 Mar 2011, 02:53 PM
Thanks for the idea, André.  I tried it out, but it skips right over recurring events.  For example, it will show regular appointments on 3/7, 3/17, and 3/20, but skip recurring appointments on 3/8 and 3/24.

Here is how I modified it to fit my situation:

foreach (Telerik.Web.UI.Appointment appointment in RadScheduler1.Appointments.Where(A => A.Start >= RadCalendar1.SelectedDates[0].Date && A.End <= RadCalendar1.SelectedDates[RadCalendar1.SelectedDates.Count - 1].Date.AddHours(23).AddMinutes(59)))
{
    lblEvents.Text += appointment.Subject + " (" + appointment.Start.ToShortDateString() + ")<br />";
}
0
Adrian Barnes
Top achievements
Rank 1
answered on 15 Mar 2011, 06:39 PM
So is this something I should open a support ticket about?  I really need to get this issue resolved. Thanks!
0
Peter
Telerik team
answered on 18 Mar 2011, 02:39 PM
Hello Adrian,

Here is a couple more alternatives you can consider:

<telerik:RadScheduler runat="server" ID="RadScheduler1" Height="0px" Width="0px"
       onappointmentcreated="RadScheduler1_AppointmentCreated" SelectedView="MonthView">
   </telerik:RadScheduler>
protected void RadScheduler1_AppointmentCreated(object sender, AppointmentCreatedEventArgs e)
   {
       Response.Write(e.Appointment.Start + "</br>");
   }

Greetings,
Peter
the Telerik team
Tags
Scheduler
Asked by
Adrian Barnes
Top achievements
Rank 1
Answers by
André
Top achievements
Rank 1
Adrian Barnes
Top achievements
Rank 1
Peter
Telerik team
Share this question
or