Search code examples
c#wpf.net-4.0datagridsortdirection

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


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


Solution

  • You can set your sort on the column itself

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn SortMemberPath="Name" SortDirection="Descending" />
        </DataGrid.Columns>
    </DataGrid>