How to set SortDirection of a WPF datagrid as Ascending on datagrid initialization

8.8k views Asked by At

I have a simple datagrid which can be sorted by clicking the column header. When I open the window, datagrid is not sorted. Then, when I click, arrow appears and it's sorted asc, click again - sorted desc etc. very simple.

What I need to do, is to open window with already sorted datagrid. So arrows (that shows which direction datagrid is sorted) should be always visible. I have to have my datagrid sorted all the time. How can I do that? I prefer to do that in xaml, but any solution will be great.

BTW. App in .NET4

3

There are 3 answers

0
Rachel On BEST ANSWER

You can set your sort on the column itself

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn SortMemberPath="Name" SortDirection="Descending" />
    </DataGrid.Columns>
</DataGrid>
0
Davide Piras On

it depends on the design of your grid, you can do this in XAML directly, for example having an entry like this:

<Trigger Property="SortDirection" Value="Ascending">

check this page:

Styling Microsoft’s WPF datagrid

0
stukselbax On

DataGridColumn.SortDirection. This property gets or sets direction of the sorting.