Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagrid in WPF - 1 column default sorted

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 ?

like image 240
Dante1986 Avatar asked Jan 22 '12 17:01

Dante1986


2 Answers

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.

like image 113
SomeInternetGuy Avatar answered Sep 22 '22 13:09

SomeInternetGuy


I described how to sort in code by first of the columns here: Initial DataGrid Sorting

You could adapt the code to sort by your specific desired column, although the whole approach seems messy.

If you want to do it in XAML ... what may work is setting CollectionViewSource.SortDescriptions:

<CollectionViewSource x:Key="cvs" Source="{StaticResource myItemsSource}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="MyPropertyName" Direction="Ascending"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

But I've never tried the latter.

like image 34
doblak Avatar answered Sep 19 '22 13:09

doblak