[]
        
(Showing Draft Content)

Sorting

By default properties and methods are listed alphabetically in the PropertyGrid control, similar to the Alphabetic view in the Visual Studio Properties window. However, you can customize the way members are listed in PropertyGrid by setting the PropertySort property. The PropertySort property can sort the properties in one of the following ways using the PropertySort enumeration:

  • Alphabetical: Sorts properties in alphabetical order. This is the default setting and appears similar to the Alphabetic view in the Visual Studio Properties window
  • Categorized: Sorts categories of the properties in an alphabetical list. The properties in each category are displayed in no particular order (the order in which are retrieved from the SelectedObject).
  • CategorizedAlphabetical: Sorts the categories as well as properties in an alphabetical order. This appears similar to the Categorized view in the Visual Studio Properties window.
  • CategorizedCustom: Sorts categories in an alphabetical list wherein the properties inside each category are displayed in a custom order defined by the user using the Display.Order attribute.
  • Custom: Sorts properties in custom order defined by the user using the Display.Order attribute.
  • NoSort: Does not sort the properties so the properties are displayed in the order in which they are retrieved from the SelectedObject.

Set the C1PropertyGrid.PropertySort property to one of the above options to customize the way the property grid is sorted. The following images show how the PropertyGrid appears before sorting and after categorical custom sorting.

PropertyGrid before sorting PropertyGrid with Categorized properties PropertyGrid after sorting PropertyGrid after sorting properties

The following code demonstrates how the properties are sorted in PropertyGrid after applying categorical custom sorting:

public partial class Sorting : Window
{
    public Sorting()
    {
        InitializeComponent();
        propertyGrid.SelectedObject = new Person()
        {
            UserName = "john_241",
            FirstName = "John",
            LastName = "Paul",
            DOB = new DateTime(1987, 2, 23),
            Email = "johnpaul241@gmail.com",
            Password = "john_@109$"
        };
        propertyGrid.PropertySort = PropertySort.CategorizedCustom;
    }
}
public class Person
{
    //Order property for custom sorting
    //GroupName property categorizes the properties in PropertyGrid

    [Display(Order = 2, GroupName = "Personal")]
    public string FirstName { get; set; }
    [Display(Order = 3, GroupName = "Personal")]
    public string LastName { get; set; }
    [Display(Order = 1, GroupName = "Personal")]
    public string UserName { get; set; }
    [Display(Order = 5, GroupName = "Personal")]
    public DateTime DOB { get; set; }
    [Display(Order = 6, GroupName = "Login")]
    public string Password { get; set; }
    [System.ComponentModel.DataAnnotations.Display(Order = 4, GroupName = "Login")]
    public string Email { get; set; }
}