This is a migrated thread and some comments may be shown as answers.

Apply sorting through ViewModel

1 Answer 224 Views
GridView
This is a migrated thread and some comments may be shown as answers.
haagel
Top achievements
Rank 1
haagel asked on 26 May 2011, 11:44 AM
I'm using the RadGridView in an MVVM application. The SortingState of each column is bound to a property (one per column) in my ViewModel. When I set a new value to these properties in code I expect the sorting of the corresponding column to be updated. However, that doesn't work as expected. The column header indicates that the column is ordered as I specified, but the rows are not ordered. Also, if I update the data in the GridView the sorting I set through code completely disappears.

I've found one perticular interesting scenario:
1. I start the application and load some data into the GridView.
2. I sort the data by column A by clicking the header of this column (works fine).
3. I press a button that invokes a command in the ViewModel that sets the SortingState of column A to SortingState.None (removes the sorting that is) and sets the SortingState of column B to SortingState.Ascending
4. The GridView header now indicates that the data should be sorted ascending by column B and not sorted by any other column. However, the rows are still sorted as before (by column A).
5. Now I fetch some new data (completely replace the old data).
6. The GridView now reverts into my previous sorting order, that is to sort by column A. This is both indicated by the GridView header and by the way the rows are actually sorted.

What am I doing wrong?

Here's how my XAML looks:
<tgv:RadGridView ItemsSource="{Binding Path=DataRows, Mode=OneWay}" AutoGenerateColumns="False" Grid.Row="0">
    <tgv:RadGridView.Columns>
        <tgv:GridViewDataColumn Header=Column A"
            DataMemberBinding="{Binding Path=Id}"
            SortingState="{Binding Path=SortingStateColumnA, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <tgv:GridViewDataColumn Header="Column B"
            DataMemberBinding="{Binding Path=FirstName}"
            SortingState="{Binding Path=SortingStateColumnB, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </tgv:RadGridView.Columns>
</tgv:RadGridView>
<Button Grid.Row="1" Command="{Binding Path=ApplySortOrderCommand, Mode=OneTime" />

And the ViewModel:
public class MainWindowViewModel : ViewModelBase
{
    private RadObservableCollection<DataRowViewModel> _dataRows;
    private RelayCommand _applySortOrderCommand;
    private SortingState _sortingStateColumnA = SortingState.None;
    private SortingState _sortingStateColumnB = SortingState.None;
 
    public MainWindowViewModel()
    {
        _dataRows = new RadObservableCollection<DataRowViewModel>();
    }
 
    public RadObservableCollection<DataRowViewModel> DataRows
    {
        get
        {
            return _dataRows;
        }
    }
     
    public SortingState SortingStateColumnA
    {
        get
        {
            return _sortingStateColumnA;
        }
        set
        {
            if (value != _sortingStateColumnA)
            {
                _sortingStateColumnA = value;
                OnPropertyChanged("SortingStateColumnA");
            }
        }
    }
     
    public SortingState SortingStateColumnB
    {
        get
        {
            return _sortingStateColumnB;
        }
        set
        {
            if (value != _sortingStateColumnB)
            {
                _sortingStateColumnB = value;
                OnPropertyChanged("SortingStateColumnB");
            }
        }
    }
 
    public ICommand ApplySortOrderCommand
    {
        get
        {
            if (_applySortOrderCommand == null)
            {
                _applySortOrderCommand = new RelayCommand(param => ApplySortOrder());
            }
            return _applySortOrderCommand;
        }
    }
 
    private void ApplySortOrder()
    {
        SortingStateColumnB = SortingState.Ascending;
    }
}

Code to fetch data removed for brevity. Note that this is not my actual code so please disregard typos.

1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 26 May 2011, 03:50 PM
Hello,

 Generally sorting state of grid columns is just for visual purposes - will not sort your data. If you want have sorting synchronized between the view (grid) and your model you can use ColumnSortDescriptor.

Best wishes,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
haagel
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or