2

I have a WPF application using a WPF-Datagrid. I can click on the column header to sort columns, which works automatically. What I want is to programatically select a column at program start which is then sorted. Or to put it another way: I want to pretend a user has clicked the column header, but do it programatically, the MVVM way, omitting changes in the code behind. Is there any solution for that?

2
  • So, you still want the user to click on the column header but the sorting code be in a view model instead? Commented Jun 1, 2016 at 10:05
  • Timothy, no, I guess you missunderstood: I want the sorting performed programatically without the requirement that a user has pressed the column header. Or put it another way: a default-sorting for my datagrid. And yes, with the column header showing the correct sorting direction. Commented Jun 1, 2016 at 10:59

2 Answers 2

7

I guess the "MVVM Way" of doing this is binding your DataGrid to a CollectionView which represents your objects collection and allows you to manage sorting via SortDescription property.

For example in your viewmodel you have a collection of objects:

private ObservableCollection<Entity> _entityCollection = null;
public ObservableCollection<Entity> EntityCollection
{
    get
    {
        return _entityCollection;
    }
    set
    {
        _entityCollection = value;
        RaisePropertyChanged("EntityCollection");
        RaisePropertyChanged("CollectionView");
    }
}

Note the RaisePropertyChanged("CollectionView") above: when your collection changes, the view should be notified that the collectionview has changed as well.

So instead of binding your datagrid directly to collection you define a read-only collectionview property like this:

private CollectionView _collectionView = null;
public CollectionView CollectionView
{
    get
    {
        _collectionView = (CollectionView)CollectionViewSource.GetDefaultView(EntityCollection);
        if (_collectionView != null)
            _collectionView.SortDescriptions.Add(new SortDescription("PropertyName", ListSortDirection.Ascending));
        return _collectionView;
    }
}

Then you bind your datagrid:

<DataGrid ItemsSource="{Binding Path=CollectionView}">

Finally if you want to change the property by which the collection is sorted you should clear the collectionview's sortdescriptions and add a new one like this:

_collectionView.SortDescriptions.Clear();
_collectionView.SortDescriptions.Add(new SortDescription("NewPropertyName", ListSortDirection.Ascending));
Sign up to request clarification or add additional context in comments.

1 Comment

I have grid with column having check box. I want to display selected items in the checkbox on top. I am trying to implement this way. But sorting is not working for me @Alex
0

you can do it by using Linq query if you have list and if you are using DataTables the you can use MyRows = myTable.Select(strExpr, strSort);

First you have to select the column you want to sort with and then at viewmodel you can can use either LINQ ormyTable.Select(strExpr, strSort)

Example code

 switch(columnname)
    {
    case "name":
    break;

    case "FatherName"
    break;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.