dotnet/wpf · error · InvalidOperationException

SR.DataGrid_ProbableInvalidSortDescription

Error message

SR.DataGrid_ProbableInvalidSortDescription

What it means

DataGrid wraps an InvalidOperationException from Items.Refresh() when re-applying sort descriptions fails, rethrowing it with DataGrid_ProbableInvalidSortDescription and the original exception as InnerException. It signals that one of the SortDescriptions applied by the grid's columns is likely invalid for the bound collection (e.g. the view cannot sort that way).

Solutions

  1. Inspect InnerException to find the actual sort failure
  2. Verify SortDescriptions property paths exist on the bound item type
  3. Ensure the source collection supports sorting (use List<T> or ICollectionView with CanSort==true)
  4. Clear/rebuild Items.SortDescriptions to recover, as the catch block already clears them before rethrowing

Example fix

// before
grid.Items.SortDescriptions.Add(new SortDescription("WrongPropertyName", ListSortDirection.Ascending));
// after
grid.Items.SortDescriptions.Add(new SortDescription("ExistingPropertyName", ListSortDirection.Ascending));
Defensive patterns

Strategy: try-catch

Validate before calling

var view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
bool sortable = view != null && view.CanSort;
bool pathsValid = sortable && grid.Items.SortDescriptions.All(sd =>
    TypeDescriptor.GetProperties(grid.ItemsSource.Cast<object>().FirstOrDefault()?.GetType() ?? typeof(object)).Find(sd.PropertyName, true) != null);

Try / catch

try { grid.Items.Refresh(); }
catch (InvalidOperationException ex) { grid.Items.SortDescriptions.Clear(); log.Warn("Invalid sort description removed", ex); }

Prevention

When it happens

Trigger: Items.Refresh() throws while DataGrid refreshes sorting after a column sort change; SortDescriptions reference property paths the collection view cannot sort on (e.g. ICollectionView without sorting support or non-existent properties).

Common situations: Binding DataGrid to a collection whose view does not support sorting (e.g. IEnumerable wrapped without ListCollectionView); sorting a column bound to a property removed or renamed; dynamic data source changes invalidating sort paths.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6fca154f215d01a7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs:7103

            }

            if (CommitAnyEdit())
            {
                PrepareForSort(sortColumn);

                DataGridSortingEventArgs eventArgs = new DataGridSortingEventArgs(sortColumn);
                OnSorting(eventArgs);

                if (Items.NeedsRefresh)
                {
                    try
                    {
                        Items.Refresh();
                    }
                    catch (InvalidOperationException invalidOperationException)
                    {
                        Items.SortDescriptions.Clear();
                        throw new InvalidOperationException(SR.DataGrid_ProbableInvalidSortDescription, invalidOperationException);
                    }
                }
            }
        }

        /// <summary>
        /// Clears the sort directions for all the columns except the column to be sorted upon
        /// </summary>
        /// <param name="sortColumn"></param>
        private void PrepareForSort(DataGridColumn sortColumn)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
            {
                return;
            }

            if (Columns != null)
            {

View on GitHub (pinned to 81131a70a4)