listview sorting by clicking on column heading

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.
Post Reply
User avatar
jsira2003@yahoo.com
Posts: 58
Joined: Fri Apr 07, 2017 9:47 am

listview sorting by clicking on column heading

Post by jsira2003@yahoo.com » Wed Oct 17, 2018 3:27 pm

I know to sort in datagridview by sorting on column heading is difficult. It is easier to do this in listview? Also with listview can i just add a datasource with any array such as:

$lvtable.datasource = $myarray

thanks,
John

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: listview sorting by clicking on column heading

Post by jvierra » Wed Oct 17, 2018 3:36 pm

A DataGridView based on a table source is completely sortable on any column by default.

The ListView control is not data enabled for items. Sorts on a ListView have to be custom coded.

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: listview sorting by clicking on column heading

Post by jvierra » Wed Oct 17, 2018 3:41 pm

Example of sort and filter
Attachments
Demo-DGVSortFormat.psf
(17.58 KiB) Downloaded 6 times

User avatar
jsira2003@yahoo.com
Posts: 58
Joined: Fri Apr 07, 2017 9:47 am

Re: listview sorting by clicking on column heading

Post by jsira2003@yahoo.com » Thu Oct 18, 2018 10:24 am

Well I got it working 90%. My only issue is I have a couple columns with numbers. They were declared as float and instead of sorting numerically, they sort as string text. Any idea why a couple of my columns do not sort correctly? All my columns are floats.

thank you,
John

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: listview sorting by clicking on column heading

Post by jvierra » Thu Oct 18, 2018 11:40 am

You have to make the columns into numbers and not text. Only numeric values will sort as numbers.

Note that my example sorts the "length" column correctly.

Post Reply