Datagrid in WPF - 1 column default sorted

Dante1986 picture Dante1986 · Jan 22, 2012 · Viewed 46.6k times · Source

In a WPF I have an DataGrid with a few columns.

At default, there is 1 I want to make it sort to, but I just cant find how I can do this.

The DataGrid in XAML looks like this:

<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[Persons]}" IsReadOnly="True" LoadingRow="dg_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Element[Name].Value}" IsReadOnly="True" />
                <DataGridTextColumn Header="Score" Binding="{Binding Path=Element[Score].Value}" IsReadOnly="True" />
                <DataGridTextColumn Header="Date" Binding="{Binding Path=Element[Date].Value}" IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>

And the only code behind it is:

public ScoreBoard()
{
    InitializeComponent();
    DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml");
    XElement TrackList = XElement.Parse(ds.GetXml());
    LibraryView.DataContext = TrackList;
}

What I just can't find is how to make it by default sorted on the "Score" column.

Can anyone help me out pointing me in the right direction ?

Answer

SomeInternetGuy picture SomeInternetGuy · Jan 10, 2014

NOTE: Using a CollectionViewSource will provide you with more power and control in these situations. When you're learning WPF I recommend understanding how to use CollectionViewSource to solve this problem along with other collection related problems like Grouping and Filtering.

EDIT: This may be due to changes in the specification. This answer is based upon using .NET 4.0, I've not researched whether this solution will work in older versions of the framework.

Given this XAML

<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[Persons]}" IsReadOnly="True" LoadingRow="dg_LoadingRow">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Element[Name].Value}" IsReadOnly="True" />
            <DataGridTextColumn Header="Score" Binding="{Binding Path=Element[Score].Value}" IsReadOnly="True" />
            <DataGridTextColumn Header="Date" Binding="{Binding Path=Element[Date].Value}" IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>

All you need to do is pick a column and specify a sorting direction for that column.

<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[Persons]}" IsReadOnly="True" LoadingRow="dg_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Element[Name].Value}" IsReadOnly="True" />
                <DataGridTextColumn Header="Score" Binding="{Binding Path=Element[Score].Value}" IsReadOnly="True" SortDirection="Ascending" />
                <DataGridTextColumn Header="Date" Binding="{Binding Path=Element[Date].Value}" IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>

This will default the sorting to the 2nd column in the ascending direction.