Apply sorting through ViewModel

2 posts, 0 answers
  1. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 26 May 2011 Link to this post

    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.
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 26 May 2011 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top