Using DataGridView unable to sort

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 5
Last visit: Mon Feb 03, 2020 11:24 am

Using DataGridView unable to sort

Post by »

When I run the following command in Exchange powershell it get the expected output..

When put into a DataGridView I get incorrect output, it seems like it doesn't sort descending


Code: Select all

$MBsizes = Get-MailboxStatistics -Database Mailboxes |
	where  { $_.Displayname -notlike "SystemMail*" -And $_.Displayname -notlike "HealthMailbox*" -And $_.DisplayName -notlike "In-Place*" } |
	sort TotalItemSize -Descending | select -first 5| Select DisplayName, TotalItemSize, StorageLimitStatus
	Update-DataGridView -DataGridView $datagridviewResults -Item $MBsizes -AutoSizeColumns DisplayedCells

Posts: 14876
Last visit: Fri Jul 30, 2021 8:16 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: Using DataGridView unable to sort

Post by jvierra »

"TotalItemSize" is not a number it is an object. It is a string and strings don't sort correctly.

Run at a prompt and inspect the object.

Get-MailboxStatistics -Database Mailboxes | select -expand TotalItemSize