How to stop the sort without make the datagridrows return to its original state

Nov 19, 2008 at 8:35 AM
Edited Nov 19, 2008 at 8:37 AM
Our application need allow user to change the order of DataGridRows by draging and droping.
We implemented this function and it works well when the data is not sorted.
But this does not work when one column  header of the DataGrid is clicked, that's it, data is sorted.
The changing row order by drag&drop will not work, i think it is because the data will be re-sorted immediately after position of the move data items in data source is moved by drag and drop.Which make it look like the drag&drop does not work.

I know I can cancel the sort by following code.But the order of DataGridRows will restore to its original states.
            System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);

            foreach (Microsoft.Windows.Controls.DataGridColumn dgColumn in dataGrid1.Columns)
                dgColumn.SortDirection = null;

But what I need is allow user to sort the datagrid, after that, user can also change the row order by drag&drop.
This means I need figure out a way to stop the sort of SortDescriptions, at the same time keep the current order of rows.
If I use SortDescriptions.Clear(), the datagrid will return to its original states, but I want it to keep current sort status after stop.
How can I make datagrid do this?
Nov 19, 2008 at 7:08 PM
DataGrid has an event named Sorting, which is raised when the column header is clicked before performing the default sort. This event is passed an instance of DataGridSortingEventArgs as a parameter and if you set Handled boolean of that instance to true, default sorting wouldn't happen. Following is the example...

<dg:DataGrid Sorting="MySorting"...>

void MySorting(object sender, DataGridSortingEventArgs e)
    // Do the sorting how ever you want to do, the default was to just add the appropriate sort descriptions
    e.Handled =  true;
Nov 24, 2008 at 3:09 AM
Thank you for your answer.
But custom sort is not what I want.
I need a way to pause the built-in column sort, at the same time, the sort state of built-in column sort is not restored to data's orginal state.
For example, I want following data in DataGrid.

ID  Name
1   Mike
2   Tom
3   John

If the second Column header Name is clicked, the data will be displayed in following order.

ID  Name
3   John
1   Mike
2   Tom

Then I hope the sort can stop work without restore the order to original state,
after that I want to change the data in the datasource, for example, swap the data in index 1 and 3.
If the sort is still working, this change in the datasource cannot be noticed on the UI, because the new data is sorted immediately.

Nov 26, 2008 at 3:54 AM
Hi jetsun,

I understand your problem and custom sort is the only answer for your case. should provide you into more insight by what I mean by custom sorting (this approach is based on the Sorting event I spoke about earlier). But following is the brief explantion of the solution in your context....

  1. First of all you cannot use sort descriptions at all, because if you have sort descriptions the CollectionView will try to enforce them and hence your drag-drop wouldnt work. Even if you clear the SortDescriptions the CollectionView will try act as a good samaritan and will try to restore your original order. Hence dont use SortDescriptions at all. Instead use an approach similar to that described in blog post of the above link.
  2. Now if you dont use SortDescriptions, your rows will not be automatically sorted immediately after the rows are dragged and dropped. But there is a price to pay. You will need to track the sort status and re-initiate sort operation after the drop operation is completed (and you are done with what ever you want to do) if the DataGrid was sorted earlier. You can use SortDirection property of DataGridColumn to track the sort status (the only catch being that it wouldnt work that good when multiple columns are sorted at the same time) or you can track it some where else using some AttachedProperty or some status object.

Hope this helps!!