Index is out of range even though debugger says it is not?

Lettuce 121 Reputation points
2020-08-24T23:50:33.067+00:00

Hi all,

So I am using a uwp datagrid control, this one: https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/datagrid

The ItemsSource of my grid is bound to an ObservableCollection. The user can click on an add button which inserts a new item to the collection: AllPlans.Insert(0, newLessonPlan);

It works well and good at first, until the user clicks on a toggle control which changes the collection, the toggle event:

private void ToggleOldEvents_Toggled(object sender, RoutedEventArgs e)  
        {  
            AllPlans = new ObservableCollection<Entities.LessonPlan>(PlanController.GetAll(((Entities.AppUser)UserBox.SelectedItem).id, ToggleOldEvents.IsOn).OrderByDescending(p => p.DateTimeFrom));  
            PlansGrid.ItemsSource = AllPlans;  
            foreach (var col in PlansGrid.Columns)  
            {  
                col.SortDirection = null;  
            }  
        }  

Then when the user tries to add another item to the collection, it falls over at AllPlans.Insert(0, newLessonPlan); It is saying that the index 0 is out of bounds.

Stack trace:

   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()  
   at System.Collections.Generic.List`1.RemoveAt(Int32 index)  
   at Microsoft.Toolkit.Uwp.UI.Controls.DataGridInternals.DataGridDisplayData.UnloadScrollingElement(Int32 slot, Boolean updateSlotInformation, Boolean wasDeleted)  
   at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.RemoveNonDisplayedRows(Int32 newFirstDisplayedSlot, Int32 newLastDisplayedSlot)  
   at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.UpdateDisplayedRows(Int32 newFirstDisplayedSlot, Double displayHeight)  
   at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.ComputeScrollBarsLayout()  
   at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.OnAddedElement_Phase2(Int32 slot, Boolean updateVerticalScrollBarOnly)  
   at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.InsertRowAt(Int32 rowIndex)  
   at Microsoft.Toolkit.Uwp.UI.Data.Utilities.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)  
   at Microsoft.Toolkit.Uwp.UI.Data.Utilities.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(EffectiveNotifyCollectionChangedAction action, Object oldItem, Object newItem, Int32 adjustedOldIndex, Int32 adjustedNewIndex)  
   at Microsoft.Toolkit.Uwp.UI.Data.Utilities.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex)  
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)  
   at TeachItUWP.Pages.LessonPlanningPage.AddEvent_Click(Object sender, RoutedEventArgs e) in C:\Users\pavel\source\repos\TeachItUWP\TeachItUWP\Pages\LessonPlanningPage.xaml.cs:line 270  

If I comment out the PlansGrid.ItemsSource = AllPlans; in my toggle event, I don't get the error, but then the user doesn't see the items in the collection on the grid.

Using AllPlans.Add(newLessonPlan); works, but I was trying to use PlansGrid.ScrollIntoView(newLessonPlan, null); which gives me System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' after the same user behaviour described earlier.

Anyone had something like this?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,206 Reputation points
    2020-08-26T02:59:20.34+00:00

    Hello,

    Welcome to Microsoft Q&A!

    By testing your code, it should be the rendering issue from the RowDetails, you could first set the RowDetailsVisibilityMode of DataGrid as Collapsed and then insert the new data. After that, set the RowDetailsVisibilityMode of DataGrid as Visible to display it. For example:

    .cs:

    private void AddOne_Click(object sender, RoutedEventArgs e)
    {
        TestGrid.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
        var randomGenerator = new Random();
    
        var entitity1 = new TestEntity();
        entitity1.Name = randomGenerator.Next(10, 999999).ToString();
        entitity1.Description = "New Random";
        CollectionOfEntities.Insert(0, entitity1);
        TestGrid.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Visible;
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful