Back to Devexpress

How to: Sort Properties

windowsforms-8370-controls-and-libraries-property-grid-how-to-sort-properties-using-the-custompropertydescriptors-event.md

latest6.8 KB
Original Source

How to: Sort Properties

  • Nov 12, 2020
  • 3 minutes to read

The PropertyGridControl displays properties in alphabetical order based on their display names. This example handles the CustomPropertyDescriptors event to sort properties in the control. The event’s Properties parameter specifies the ordered collection of properties.

View Example

csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraVerticalGrid.Events;
using System.Collections;
using System.Globalization;

namespace PropertySorting {
    public partial class Form1 : Form {
        bool allowCustomSorting = false;

        public Form1() {
            InitializeComponent();
            this.propertyGridControl1.SelectedObject = new CustomClass() { Property1 = "one", Property2 = "two", Property3 = "three", Property4 = "four", Property5 = "five" };
        }

        void propertyGridControl1_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) {
            if(allowCustomSorting && e.Context.PropertyDescriptor == null) {
                e.Properties = e.Properties.Sort(new string[] { "Property5", "Property4", "Property3", "Property2", "Property1" });
            }
        }
        void button1_Click(object sender, EventArgs e) {
            allowCustomSorting = !allowCustomSorting;
            propertyGridControl1.OptionsBehavior.PropertySort = allowCustomSorting ? DevExpress.XtraVerticalGrid.PropertySort.NoSort : DevExpress.XtraVerticalGrid.PropertySort.Alphabetical;
            propertyGridControl1.Refresh();
        }
    }
    class CustomClass {
        [DisplayName("C")]
        public string Property1 { get; set; }
        [DisplayName("B")]
        public string Property2 { get; set; }
        [DisplayName("A")]
        public string Property3 { get; set; }
        [DisplayName("D")]
        public string Property4 { get; set; }
        [DisplayName("E")]
        public string Property5 { get; set; }
    }
}
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace PropertySorting {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraVerticalGrid.Events
Imports System.Collections
Imports System.Globalization

Namespace PropertySorting
    Partial Public Class Form1
        Inherits Form
        Private allowCustomSorting As Boolean = False

        Public Sub New()
            InitializeComponent()
            Me.propertyGridControl1.SelectedObject = New CustomClass() With {.Property1 = "one", .Property2 = "two", .Property3 = "three", .Property4 = "four", .Property5 = "five"}
        End Sub

        Private Sub propertyGridControl1_CustomPropertyDescriptors(ByVal sender As Object, ByVal e As CustomPropertyDescriptorsEventArgs) Handles propertyGridControl1.CustomPropertyDescriptors
            If allowCustomSorting AndAlso e.Context.PropertyDescriptor Is Nothing Then
                e.Properties = e.Properties.Sort(New String() { "Property5", "Property4", "Property3", "Property2", "Property1" })
            End If
        End Sub
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
            allowCustomSorting = Not allowCustomSorting
            propertyGridControl1.OptionsBehavior.PropertySort = If(allowCustomSorting, DevExpress.XtraVerticalGrid.PropertySort.NoSort, DevExpress.XtraVerticalGrid.PropertySort.Alphabetical)
            propertyGridControl1.Refresh()
        End Sub
    End Class
    Friend Class CustomClass
        Private privateProperty1 As String
        <DisplayName("C")> _
        Public Property Property1() As String
            Get
                Return privateProperty1
            End Get
            Set(ByVal value As String)
                privateProperty1 = value
            End Set
        End Property
        Private privateProperty2 As String
        <DisplayName("B")> _
        Public Property Property2() As String
            Get
                Return privateProperty2
            End Get
            Set(ByVal value As String)
                privateProperty2 = value
            End Set
        End Property
        Private privateProperty3 As String
        <DisplayName("A")> _
        Public Property Property3() As String
            Get
                Return privateProperty3
            End Get
            Set(ByVal value As String)
                privateProperty3 = value
            End Set
        End Property
        Private privateProperty4 As String
        <DisplayName("D")> _
        Public Property Property4() As String
            Get
                Return privateProperty4
            End Get
            Set(ByVal value As String)
                privateProperty4 = value
            End Set
        End Property
        Private privateProperty5 As String
        <DisplayName("E")> _
        Public Property Property5() As String
            Get
                Return privateProperty5
            End Get
            Set(ByVal value As String)
                privateProperty5 = value
            End Set
        End Property
    End Class
End Namespace
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms

Namespace PropertySorting
    Friend NotInheritable Class Program
        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        Private Sub New()
        End Sub
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace