0

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....

4
  • 2
    It's PropertyName and Direction, not ColumnName and SortDirection: $Datagrid.Items.SortDescription.Add([System.ComponentModel.SortDescription]@{PropertyName="MyColumn"; Direction="Ascending"}) Commented Mar 2, 2022 at 14:25
  • Throws an error: Exception during call of "Add" with 1 argument: the collection has fixed size Commented Mar 2, 2022 at 14:28
  • 2
    There's an s missing (it's SortDescriptions, not SortDescription) Commented Mar 2, 2022 at 14:32
  • It works! damn typos. Where would you find the names anyway? i am having issues scraping microsoft documentation Commented Mar 2, 2022 at 14:36

2 Answers 2

2

Try this:

$sortDescription = New-Object System.ComponentModel.SortDescription('MyColumn', 'Ascending')
$Datagrid.Items.SortDescriptions.Add($sortDescription)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! i like Mathias answer cause it's a one liner but you actually cleared a doubt i had about how to handle objects and creating them.
0

In C# the solution is this, I hope this can help you to understand how WPF works.

To sort your DataGrid like if you clicked on your column 3, you have to work on DataView created from WPF. In this exampe the DataGrid is sorted every time that columns are AutoGenerated but you can select other event like DataGrid1_Loaded

using System.Windows.Data;

private void DataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
    {
        (((DataGrid)sender).ItemsSource as DataView).Sort = DataGrid1.Columns[3].Header.ToString();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.