Skip to content

[Bug] Exception is thrown when removing the last item in a group in ObservableGroupedCollection bound to a DataGrid #4473

Open
@phizch

Description

@phizch

Description

When the item at the last index of an ObservableGroup that is in an ObservableGroupedCollection bound via a CollectionViewSource to a DataGrid is removed, an ArgumentOutOfRangeExcpetion is thrown.

Short example:

var list = new List<int> { 1, 1, 1, 2, 3 };
var ogc = new ObservableGroupedCollection<int,int>( list.GroupBy( x => x ) );
var cvs = new CollectionViewSource { Source = ogc, IsSourceGrouped = true };
ogc[0].RemoveAt( ogc[0].Count-1 ); // works fine
var myDataGrid = new DataGrid();
myDataGrid.ItemsSource = cvs.View;
ogc[0].RemoveAt( ogc[0].Count-1 ); // ArgumentOutOfRangeExcpetion

Expected behavior

No exception should be thrown.
The DataGrid should remove the row, and if the row was selected, either select the next or previous row, or set selection to empty.

Partial fix

The DataGrid should check that the index is still valid before trying to fetch the value from the ObservableGroup.

Workaround

Wrapping the Remove or RemoveAt in a try/catch block and catching the ArgumentOutOfRangeExcpetion mitigates the issue.

try
{
    ogc[0].RemoveAt( ogc[0].Count-1 ); // ArgumentOutOfRangeExcpetion
}
catch ( ArgumentOutOfRangeExcpetion )
{
    //todo: log the exception
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DataGrid 🔠Issues on DataGrid controlbug 🐛An unexpected issue that highlights incorrect behaviorcontrols 🎛️help wantedIssues identified as good community contribution opportunities

    Type

    No type

    Projects

    • Status

      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions