Datagrid Column Sorting the Generating Error

I have a datagrid column whose column values ​​are database bound. I used DataGridTemplateColumn and I need to use sorting in this column.

my:DataGridTemplateColumn SortMemberPath="FileName" Header="Name" IsReadOnly="True" MinWidth="150"

It works and sorts the data, but when I edit the data after sorting, I need to regenerate the data in the column.

FileListingGrid.ItemsSource = listFiles1;

But this generates "Sort" is not allowed during an AddNew or EditItem transaction.

It works fine when the column data is not sorted, but whenever I sort the data and you need to regenerate the column data, it gives the following error.

+3
source share
3

ListCollectiontView.AddNewItem( item ); ListCollectiontView.CommitNew(); . CommitEdit()

+2

.

1) CommitNew() CommitEdit()

private void DataGrid_ParametersList_Sorting(object sender, DataGridSortingEventArgs e)
{
DataGridColumn column = e.Column;

//prevent the built-in sort from sorting
e.Handled = true;

ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;

//set the sort order on the column
column.SortDirection = direction;

//use a ListCollectionView to do the sort.
ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(DataGrid_ParametersList.ItemsSource);

ParametersListComparer customComparer = new ParametersListComparer();
customComparer.SortDirection = direction;
customComparer.SortMemeberPath = column.SortMemberPath;

if (lcv.IsAddingNew) 
lcv.CommitNew();
if (lcv.IsEditingItem)
lcv.CommitEdit();

//apply the sort
lcv.CustomSort = customComparer;
}

2) .

<my:DataGrid x:Name="DataGrid"                             
IsReadOnly="True"
Sorting="DataGrid_Sorting">
+2

Can you use CollectionViewSource and specify your SortMemberPath (in your case "FileName") as a SortDescription?

0
source

Source: https://habr.com/ru/post/1767508/


All Articles