Custom WPF DataGrid sorting with some records blocked

I have a WPF Datagrid with custom sorting and it works great. I need to lock some entries in the collection to Top and Bottom (Top Lock and Bottom Lock). Therefore, when a particular record is locked at the top, it should be the first record in the collection, regardless of any value for the selected column (i.e., I do not want to sort those rows that are locked at the top and bottom). Here is my sorting method.

private void PerformLineCustomSort(DataGridColumn column)
        {
            ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
            column.SortDirection = direction;
            ListCollectionView listCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(Lines);

            LineSorter lineSort = new LineSorter(direction, column);
            listCollectionView.CustomSort = lineSort;

        }

where LineSorter is my custom sorter class with IComparer implementation . It works great. Now I want to apply Top Lock and Bottom lock to the ListCollectionView. I tried the following code with the above, but it has no changes (i.e. the List is sorted based on only a custom sort column).

listCollectionView.SortDescriptions.Add(new SortDescription("TopLock", ListSortDirection.Descending));
listCollectionView.SortDescriptions.Add(new SortDescription("BotLock", ListSortDirection.Ascending));

Is there a way to make this script or do I need to manually delete and then add the locked records. Please suggest ..

+5
source share
1 answer

Well. You can do this from your code.

Let's pretend that

The page size         = 10 

Current page          = 2

Collection name       = myItems

First Item to display = initialItem

Last Item to display  = lastItem

Steps:

  • Collection sort
  • Take from 9 to 16 items (8 items) from the collection (Because you are on the second page)

= 8 x ( -1) + 1

number = 8 x

:

= 8 x (2 - 1) + 1 = 9

= 8 x 2 = 16

( 8 , )

  • intialItem

myItems.Insert(0, initialItem);

  • lastItem

    myItems.Insert(9, lastItem);

0

All Articles