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.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: Sorting by column in a datagridview

Post by mtartaglia »

$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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Sorting by column in a datagridview

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Sorting by column in a datagridview

Post by jvierra »

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.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked