Index out of range when adding appointments

7 posts, 0 answers
  1. Joel
    Joel avatar
    41 posts
    Member since:
    Nov 2010

    Posted 06 Jun 2011 Link to this post

    The exact exception is this: "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"

    The first time I call CreateNew on my ScheduleView, the IAppointment object is created and I'm able to set its properties. But any further attempt to touch the AppointmentsSource seems to result in the above exception. Am I doing something wrong or is this a bug?

    MainPage.xaml
    <UserControl x:Class="SVIssue.MainPage"
    xmlns:SVIssue="clr-namespace:SVIssue"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
    <SVIssue:GraphTestViewModel x:Key="GraphTestViewModel"/>
    <telerik:OrientedGroupHeaderStyleSelector x:Key="GroupHeaderStyleSelector">
    <telerik:OrientedGroupHeaderStyleSelector.VerticalBottomLevelStyle>
    <Style TargetType="telerik:GroupHeader">
    <Style.BasedOn>
    <Style TargetType="telerik:GroupHeader">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="FontStyle" Value="Italic"/>
    </Style>
    </Style.BasedOn>
    </Style>
    </telerik:OrientedGroupHeaderStyleSelector.VerticalBottomLevelStyle>
    </telerik:OrientedGroupHeaderStyleSelector>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition/>
    </Grid.RowDefinitions>
    <Button x:Name="btnAddData" Content="Add Data" HorizontalAlignment="Center" Click="btnAddData_Click"/>
    <telerik:RadScheduleView x:Name="svTest"
    Grid.Row="1"
    DataContext="{StaticResource GraphTestViewModel}"
    AppointmentsSource="{Binding Appointments}"
    ResourceTypesSource="{Binding ResourceTypes}"
    GroupHeaderStyleSelector="{StaticResource GroupHeaderStyleSelector}">
    <telerik:RadScheduleView.ViewDefinitions>
    <telerik:DayViewDefinition Orientation="Horizontal" GroupHeaderDateStringFormat="{}{0:dddd}"/>
    <telerik:WeekViewDefinition Orientation="Horizontal"/>
    <telerik:TimelineViewDefinition Orientation="Horizontal"/>
    </telerik:RadScheduleView.ViewDefinitions>
    <telerik:RadScheduleView.GroupDescriptionsSource>
    <telerik:GroupDescriptionCollection>
    <telerik:DateGroupDescription />
    <telerik:ResourceGroupDescription ResourceType="Speaker" />
    <telerik:ResourceGroupDescription ResourceType="Room" />
    </telerik:GroupDescriptionCollection>
    </telerik:RadScheduleView.GroupDescriptionsSource>
    </telerik:RadScheduleView>
    </Grid>
    </UserControl>

    MainPage.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls.ScheduleView;
    namespace SVIssue
    {
    public partial class MainPage : UserControl
    {
    public MainPage()
    {
    InitializeComponent();
    }
    private void btnAddData_Click(object sender, RoutedEventArgs e)
    {
    DateTime currentTime = DateTime.Today.AddHours(1);
    int count = 0;
    while (currentTime < DateTime.Today.AddDays(1))
    {
    IAppointment appointment = this.svTest.CreateNew();
    appointment.Start = currentTime;
    appointment.End = currentTime.AddMinutes(59).AddSeconds(59);
    appointment.Subject = String.Format("New appointment {0}", count++);
    //appointment.Resources.Add(this.Resources[0].Resources[0]);
    currentTime = currentTime.AddHours(1);
    }
    this.svTest.Commit();
    }
    }
    }

    GraphTestViewModel.cs
    using System;
    using System.Collections.ObjectModel;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.ScheduleView;
    namespace SVIssue
    {
    public class GraphTestViewModel
    {
    public ObservableCollection<Appointment> Appointments { get; set; }
    public ObservableCollection<ResourceType> ResourceTypes { get; set; }
    public GraphTestViewModel()
    {
    this.Appointments = new ObservableCollection<Appointment>();
    this.ResourceTypes = this.GenerateResourceTypes();
    }
    private ObservableCollection<ResourceType> GenerateResourceTypes()
    {
    ObservableCollection<ResourceType> newResourceTypes = new ObservableCollection<ResourceType>();
    ResourceType roomType = new ResourceType("Room");
    Resource room102 = new Resource("Room 102");
    Resource room203 = new Resource("Room 203");
    Resource room406 = new Resource("Room 406");
    roomType.Resources.Add(room102);
    roomType.Resources.Add(room203);
    roomType.Resources.Add(room406);
    ResourceType speakerType = new ResourceType("Speaker");
    Resource tomSpeaker = new Resource("Tom");
    Resource peterSpeaker = new Resource("Peter");
    speakerType.Resources.Add(tomSpeaker);
    speakerType.Resources.Add(peterSpeaker);
    newResourceTypes.Add(roomType);
    newResourceTypes.Add(speakerType);
    return newResourceTypes;
    }
    }
    }
  2. Joel
    Joel avatar
    41 posts
    Member since:
    Nov 2010

    Posted 06 Jun 2011 Link to this post

    Instead of creating the IAppointment using CreateNew, I created a new Appointment and added it to my ViewModel directly.

    This fixed the error, but I would still like to know which way is the correct way to add appointments.  If both are permissable, when is CreateNew appropriate?
  3. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 07 Jun 2011 Link to this post

    Hello Joel,

    Both ways are correct for adding a new appointment to the source collection. When the source collection implement INotifyCollectionChanged (like ObservableCollection) then ScheduleView will update. If the source collection does not implement INotifyCollectionChanged then the only option is to use ScheduleView.CreateNew() method.

    The problem is that there is a bug in ListCollectionView - if collection view is grouped on more than 1 level and filtered (e.g. Day, Week, Month or Timeline viewdefinition with resources) then adding new item using CreateNew method throws exception. We have reported it to Microsoft and still waiting for a fix.

    So to summarize you could add appointments directly to your source collection if it is ObservableCollection, or you could add appointments using CreateNew method if there is at least 1 appointment in the visible range.

    Regards,
    Hristo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Michael Lilly
    Michael Lilly avatar
    53 posts
    Member since:
    Apr 2010

    Posted 04 Oct 2011 Link to this post

    My problem isn't related to this thread at all, it's just similar in error and how to get that error.
  5. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 05 Oct 2011 Link to this post

    Hello Michael,

    I suggest you open a support thread and send us simple running project illustrating the problem to test it locally. We will review it and provide you  with more details.

    Regards,
    Rosi
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Michael Lilly
    Michael Lilly avatar
    53 posts
    Member since:
    Apr 2010

    Posted 05 Oct 2011 Link to this post

    Sorry, I wasn't very clear on that post.  I initially posted my problem because I thought it was related to his.  I since figured out that it isn't related so edited out my details.  I should have explained myself better.

    I'm still researching my issue, it's very similar, but I'm already using an ObservableCollection.  I am getting it when I do a DialogViewModel.Confirm() call, or when I drop an item I'm dragging from another control.  I'm not on the Q2 release, yet, though, so it could be related to my version.
  7. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 07 Oct 2011 Link to this post

    Hi Michael ,

    Thank you for your response.

    If you still experience the issue I suggest you open a support thread and send us sample running project. We will test it locally and provide you with a solution or more technical details.

    Greetings,
    Rosi
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top