How to get rid of No Sort state for the column I want to sort

2 posts, 0 answers
  1. Yang Lu
    Yang Lu avatar
    5 posts
    Member since:
    May 2010

    Posted 21 Jun 2010 Link to this post

    By default each column can be sorted in 3 ways: Ascending, Descending, No Sort. How do I get rid of the third state, but have each the column can only be sorted either in ascending or descending order when I click the column header?

    Thanks,
    Yang
  2. vanya
    vanya avatar
    35 posts
    Member since:
    Nov 2008

    Posted 21 Jun 2010 Link to this post

    To apply a custom sorting you should use the Sorting event see the link below:
    But you should notice the  the following two properties:

    • OldSortingState - defines the current sorting state of the column being sorted.
    • NewSortingState - defines the new sorting state of the column and controls the sorting indicator of the column's header. Even if you don't want the built-in sorting functionality to be executed you have to properly set this property, otherwise the visual state of the sort indicator won't match the sort state of the data.
    Try the following for example:


    <Window x:Class="CustomSortingDataGrid.MainWindow" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            Title="MainWindow" Height="450" Width="565" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        <Grid> 
            <telerik:RadGridView Width="Auto" Height="Auto" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="47,19,0,0" Name="radGridView1" VerticalAlignment="Top" Sorting="radGridView1_Sorting"/> 
        </Grid> 
    </Window> 
     


    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Navigation; 
    using System.Windows.Shapes; 
    using Telerik.Windows.Controls; 
     
    namespace CustomSortingDataGrid 
        /// <summary> 
        /// Interaction logic for MainWindow.xaml 
        /// </summary> 
        public partial class MainWindow : Window 
        { 
            public MainWindow() 
            { 
                InitializeComponent(); 
                this.DataContext = (from c in Enumerable.Range(1,10) 
                                    select new Employee 
                                    { 
                                        EmployeeID = c 
                                    }).ToList(); 
                this.radGridView1.Sorting+=new EventHandler<GridViewSortingEventArgs>(radGridView1_Sorting); 
            } 
     
            private void radGridView1_Sorting(object sender, Telerik.Windows.Controls.GridViewSortingEventArgs e) 
            { 
                IEnumerable<Employee> employees = e.DataControl.ItemsSource as IEnumerable<Employee>; 
           
                if (employees == null
                { 
                    e.Cancel = true
                    return
                } 
                if (e.OldSortingState == SortingState.Ascending) 
                { 
                    e.NewSortingState = SortingState.Descending; 
                    employees = employees.OrderByDescending(employee => employee.GetType() 
                                                                       .GetProperty(e.SortPropertyName) 
                                                                       .GetValue(employee, null)); 
                } 
          
                if (e.OldSortingState == SortingState.Descending) 
                { 
                    e.NewSortingState = SortingState.Ascending; 
                    employees = employees.OrderBy(employee => employee.GetType() 
                                                                                 .GetProperty(e.SortPropertyName) 
                                                                                 .GetValue(employee, null)); 
                } 
             
                
               
                e.DataControl.ItemsSource = employees.ToList(); 
                e.Cancel = true
            } 
        } 
        public class Employee 
        { 
            public int EmployeeID { getset; } 
        } 
     


    Hope this helps!
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top