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

Index out of range when adding appointments

6 Answers 118 Views
ScheduleView
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Joel asked on 06 Jun 2011, 03:34 PM
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;
}
}
}

6 Answers, 1 is accepted

Sort by
0
Joel
Top achievements
Rank 1
answered on 06 Jun 2011, 08:52 PM
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?
0
Hristo
Telerik team
answered on 07 Jun 2011, 08:23 AM
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
0
Michael Lilly
Top achievements
Rank 1
answered on 04 Oct 2011, 08:10 PM
My problem isn't related to this thread at all, it's just similar in error and how to get that error.
0
Rosi
Telerik team
answered on 05 Oct 2011, 08:06 AM
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 >>

0
Michael Lilly
Top achievements
Rank 1
answered on 05 Oct 2011, 02:06 PM
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.
0
Rosi
Telerik team
answered on 07 Oct 2011, 11:17 AM
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 >>

Tags
ScheduleView
Asked by
Joel
Top achievements
Rank 1
Answers by
Joel
Top achievements
Rank 1
Hristo
Telerik team
Michael Lilly
Top achievements
Rank 1
Rosi
Telerik team
Share this question
or