Search code examples
wpfpowershelldatagrid

Powershell set default sorting on datagrid


So i have this wpf datagrid whose columns are defined in a xaml file, i programmatically check data and insert rows in it 1 by 1 after a button is pressed.

I'm trying to understand how i can populate it and set a sorting (the same sorting as i would have clicking on a column header)

in winforms i used to add:

$myDataGrid.Sort($myDataGrid.Columns[3],'Ascending')

after my populating function.

How do i replicate that in WFP (and powershell)?

i did try:

$Datagrid.Items.SortDescription.Add([pscustomobject]@{ColumnName="MyColumn";SortDirection="ListSortDirection.Ascending"})

but i'm having quite some trouble as i only find c# explanations and trying to adapt is not working out....


Solution

  • Try this:

    $sortDescription = New-Object System.ComponentModel.SortDescription('MyColumn', 'Ascending')
    $Datagrid.Items.SortDescriptions.Add($sortDescription)