Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

AdvancedCollectionView ArgumentOutOfRangeException when remove the last element #4263

Open

Description

@Dimigergo

Describe the bug

When I use an AdvancedCollectionView and remove the last element from the bound collection the following exception thrown: 'System.ArgumentOutOfRangeException' in System.Private.CoreLib.dll Index was out of range. Must be non - negative and less than the size of the collection.

  • Is this bug a regression in the toolkit?

Steps to Reproduce

  • Can this be reproduced in the Sample App?

Steps to reproduce the behavior:

  1. AdvancedCollectionView.zip
  2. Start application
  3. Click on 'Delete last'
  4. See error in message box.

Expected behavior

The last element should be deleted without error.

Environment

NuGet Package(s):
Package Version(s):

    <PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
      <Version>6.2.12</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Toolkit.Uwp">
      <Version>7.1.0-rc2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Toolkit.Uwp.UI">
      <Version>7.1.0-rc2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls">
      <Version>7.1.0-rc2</Version>
    </PackageReference>

Windows 10 Build Number:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • May 2020 Update (19041)
  • Insider Build ({build_number})

App min and target version:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • May 2020 Update (19041)
  • Insider Build ({build_number})

Device form factor:

  • Desktop
  • Xbox
  • Surface Hub
  • IoT

Visual Studio version:

  • 2017 (15.{minor_version})
  • 2019 (16.11.3)
  • 2022 (17.{minor_version})

Activity

  1. added
    bug 🐛An unexpected issue that highlights incorrect behavior
    on Sep 21, 2021
  2. michael-hawker commented on Sep 21, 2021

    @michael-hawker
    Member

    Think this is a duplicate of #2913, thanks for providing a minimal repro project @Dimigergo.

    Stack:

    System.Private.CoreLib.dll!System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()	Unknown
    Microsoft.Toolkit.Uwp.UI.dll!Microsoft.Toolkit.Uwp.UI.AdvancedCollectionView.this[int].get(int index) Line 214	C#
    Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.dll!Microsoft.Toolkit.Uwp.UI.Controls.DataGridInternals.DataGridDataConnection.NotifyingDataSource_VectorChanged(Windows.Foundation.Collections.IObservableVector<object> sender, Windows.Foundation.Collections.IVectorChangedEventArgs e) Line 957	C#
    Microsoft.Toolkit.Uwp.UI.dll!Microsoft.Toolkit.Uwp.UI.AdvancedCollectionView.OnVectorChanged(Windows.Foundation.Collections.IVectorChangedEventArgs e) Line 65	C#
    Microsoft.Toolkit.Uwp.UI.dll!Microsoft.Toolkit.Uwp.UI.AdvancedCollectionView.SourceNcc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) Line 629	C#
    System.ObjectModel.dll!System.Collections.ObjectModel.ObservableCollection<AdvancedCollectionView.MainPage.CValami>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)	Unknown
    AdvancedCollectionView.exe!AdvancedCollectionView.MainPage.ButtonClick(object sender, Windows.UI.Xaml.RoutedEventArgs e) Line 91	C#
    

    get:

    public object this[int index]
    {
    get { return _view[index]; }
    set { _view[index] = value; }
    }

    removeAt:

    case CollectionChange.ItemRemoved:
    if (!this.IsGrouping)
    {
    // If we're grouping then we handle this through the CollectionViewGroup notifications.
    // Remove is a single item operation.
    _owner.RemoveRowAt(index, sender[index]);
    }

    OnVectorChanged:

    private void OnVectorChanged(IVectorChangedEventArgs e)
    {
    if (_deferCounter > 0)
    {
    return;
    }
    VectorChanged?.Invoke(this, e);
    // ReSharper disable once ExplicitCallerInfoArgument
    OnPropertyChanged(nameof(Count));
    }

    case NotifyCollectionChangedAction.Remove:
    DetachPropertyChangedHandler(e.OldItems);
    if (_deferCounter <= 0)
    {
    if (e.OldItems?.Count == 1)
    {
    HandleItemRemoved(e.OldStartingIndex, e.OldItems[0]);
    }
    else
    {
    HandleSourceChanged();
    }
    }

    private void RemoveFromView(int itemIndex, object item)
    {
    _view.RemoveAt(itemIndex);
    if (itemIndex <= CurrentPosition)
    {
    CurrentPosition--;
    }
    var e = new VectorChangedEventArgs(CollectionChange.ItemRemoved, itemIndex, item);
    OnVectorChanged(e);
    }

    So, I think the issue here is DataGrid is trying to retrieve the item to know which one is removed, but it's already been removed from the collection when the event is called. Normally with an INotifyCollectionChanged event the item is provided in the event args, but that doesn't seem to be the case with the IVectorChangedEventArgs

    If we swap the DataGrid for a ListView, things work fine. This seems to work fine in Wpf as they use the collectionchangedevent compared to an observablevector. FYI @RBrid.

  3. Mirofire commented on Apr 15, 2023

    @Mirofire

    Is this issue still open? I getting the same issue with Nuget package CommunityToolkit.WinUI.UI.Controls Version: 7.1.2

  4. DavidM29 commented on Apr 3, 2025

    @DavidM29

    This has been open for 3.5 years, is the PR ever going to be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DataGrid 🔠Issues on DataGrid controlIn-PR 🚀bug 🐛An unexpected issue that highlights incorrect behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions