{"record":{"id":"6fca154f215d01a7","repo":"dotnet/wpf","slug":"sr-datagrid-probableinvalidsortdescription","errorCode":null,"errorMessage":"SR.DataGrid_ProbableInvalidSortDescription","messagePattern":"SR\\.DataGrid_ProbableInvalidSortDescription","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs","lineNumber":7103,"sourceCode":"            }\n\n            if (CommitAnyEdit())\n            {\n                PrepareForSort(sortColumn);\n\n                DataGridSortingEventArgs eventArgs = new DataGridSortingEventArgs(sortColumn);\n                OnSorting(eventArgs);\n\n                if (Items.NeedsRefresh)\n                {\n                    try\n                    {\n                        Items.Refresh();\n                    }\n                    catch (InvalidOperationException invalidOperationException)\n                    {\n                        Items.SortDescriptions.Clear();\n                        throw new InvalidOperationException(SR.DataGrid_ProbableInvalidSortDescription, invalidOperationException);\n                    }\n                }\n            }\n        }\n\n        /// <summary>\n        /// Clears the sort directions for all the columns except the column to be sorted upon\n        /// </summary>\n        /// <param name=\"sortColumn\"></param>\n        private void PrepareForSort(DataGridColumn sortColumn)\n        {\n            if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)\n            {\n                return;\n            }\n\n            if (Columns != null)\n            {","sourceCodeStart":7085,"sourceCodeEnd":7121,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs#L7085-L7121","documentation":"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).","triggerScenarios":"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).","commonSituations":"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.","solutions":["Inspect InnerException to find the actual sort failure","Verify SortDescriptions property paths exist on the bound item type","Ensure the source collection supports sorting (use List<T> or ICollectionView with CanSort==true)","Clear/rebuild Items.SortDescriptions to recover, as the catch block already clears them before rethrowing"],"exampleFix":"// before\ngrid.Items.SortDescriptions.Add(new SortDescription(\"WrongPropertyName\", ListSortDirection.Ascending));\n// after\ngrid.Items.SortDescriptions.Add(new SortDescription(\"ExistingPropertyName\", ListSortDirection.Ascending));","handlingStrategy":"try-catch","validationCode":"var view = CollectionViewSource.GetDefaultView(grid.ItemsSource);\nbool sortable = view != null && view.CanSort;\nbool pathsValid = sortable && grid.Items.SortDescriptions.All(sd =>\n    TypeDescriptor.GetProperties(grid.ItemsSource.Cast<object>().FirstOrDefault()?.GetType() ?? typeof(object)).Find(sd.PropertyName, true) != null);","typeGuard":null,"tryCatchPattern":"try { grid.Items.Refresh(); }\ncatch (InvalidOperationException ex) { grid.Items.SortDescriptions.Clear(); log.Warn(\"Invalid sort description removed\", ex); }","preventionTips":["Bind to collections whose views support sorting (CanSort) and use List<T>/ObservableCollection","Keep SortDescription property paths in sync with item type","Check InnerException for the root cause"],"tags":["wpf","datagrid","sorting","collection-view"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}