Sorting by column in a datagridview

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
miketartaglia
Posts: 97
Joined: Mon Feb 02, 2015 7:44 am

Re: Sorting by column in a datagridview

Post by miketartaglia » Wed Sep 18, 2019 4:59 pm

$currentsort ended up being null in the debugger. I set it when someone sorts a column.

Obviously I am missing something. Sorry to ask this, but I am hoping you can be a little more specific as to where your two suggestions should be put.

jvierra
Posts: 14000
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Sorting by column in a datagridview

Post by jvierra » Wed Sep 18, 2019 5:15 pm

If you would just add a DataTable and leave everything else alone my code would work as intended. It is clear that you method of creating and loading the table is not correct for proper functioning of the sort capability.
All sorting in a grid is one automatically without any code. The Sort filter gets set whenever a column is clicked. It will only be null on the initial load so you need to check and set the sort to some initial state.

jvierra
Posts: 14000
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Sorting by column in a datagridview

Post by jvierra » Wed Sep 18, 2019 5:18 pm

Also no`te that the code I posted is intended only to be used when you refresh the data, It will not work as expected if you use it in multiple events. You should have NO events managing the sort or column clicks as they will just create issues.

Locked