Donotalo
Donotalo

Reputation: 13035

Display items in PropertyGrid in the sequence they are declared

Is it possible to display PropertyGrid items in the UI in the way they are declared? I found that they are sorted first by CategoryAttribute and then by DisplayName attribute in ascending order.

I'm using .NET version 3.5 using Visual Studio 2010 Ultimate.

EDIT

The application is a WPF application.

Upvotes: 4

Views: 13859

Answers (4)

SimonTi
SimonTi

Reputation: 21

As above [System.Windows.Forms.PropertySort]::NoSort
However note if you are creating your control with a -Property parameter and setting both this PropertySort and the SelectedObject in that parameter's hashtable then it may still have the first item alphabetically as the SelectedGridItem and the grid scrolled to show that.

To ensure the new (original order's first) property is the SelectedGridItem either:
- set the SelectedObject after the control is created
- or for your control } you can use:
}.SelectedGridItem = }.SelectedGridItem.ParentGridEntry.Children[0]

Upvotes: 0

user13006049
user13006049

Reputation: 11

You can try this code.

private void propertyGrid1_PropertySortChanged(object sender, EventArgs e)
{
    if (propertyGrid1.PropertySort == PropertySort.CategorizedAlphabetical)
    {
        propertyGrid1.PropertySort = PropertySort.Categorized;
    }
}

Upvotes: 1

Max Lambertini
Max Lambertini

Reputation: 3719

From this doc: https://learn.microsoft.com/en-US/dotnet/api/system.windows.forms.propertysort

if you set the property PropertySort to PropertySort.NoSort the sorting order of properties should follow this criterium: Properties are displayed in the order in which they are retrieved from the TypeDescriptor.

Upvotes: 8

Walter Verhoeven
Walter Verhoeven

Reputation: 4431

You can set quite a few properties using annotations, one of them is the "order of display" using the System.ComponentModel.DataAnnotations.Display Attribute would look like this:

[DisplayName("Error"),Display(Order = 5)]
public string Error { get; internal set; }

Upvotes: 4

Related Questions