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

Sorting by column in a datagridview

Post by mtartaglia »

I have a datagridview from a MS-SQL database that gets displayed in a form. I used the code that the database connection object created automatically. I want to sort by one of the columns as soon as the form opens. I assume it's done in the last list in the code below. Just not sure what to change.

$datagridviewResults_ColumnHeaderMouseClick=[System.Windows.Forms.DataGridViewCellMouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
if($datagridviewResults.DataSource -is [System.Data.DataTable])
{
$column = $datagridviewResults.Columns[$_.ColumnIndex]
$direction = [System.ComponentModel.ListSortDirection]::Ascending

if($column.HeaderCell.SortGlyphDirection -eq 'Descending')
{
$direction = [System.ComponentModel.ListSortDirection]::Descending
}

$datagridviewResults.Sort($datagridviewResults.Columns[$_.ColumnIndex], $direction)
}
}
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 »

Just click the column header. If the table is loaded cor4rectly it wil sort. If it is not loaded correctly it will not sort.
You must add a DataTable to the grid to allow it to sort correctly.

$datagridview.DataSource = $datatable

"$datatable" should be the object returned from the SQL.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: Sorting by column in a datagridview

Post by mtartaglia »

Yes I know how to do that, that was way too obvious. But what I am trying to do is have the form reload using a timer (say 1 minute). But when it does reload, the sort reverts back to the data in order of when the data was entered not by the sort how you mentioned.
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 »

Set the sort for the source.

$datatable.DefaultView.Sort = 'Name' # 'Name' is column name.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: Sorting by column in a datagridview

Post by mtartaglia »

I put it in at the bottom of the previously posted code, but since it's not working, I assume that is not the correct place.
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 »

When you reload the data add it to the DataTable at that point. Remove the click event.
You an also just add an "ORDER BY" to your SQL and the table will be sorted when it is retrieved.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: Sorting by column in a datagridview

Post by mtartaglia »

Awesome. I will try that. On another note, is there a way for the form to "reload" and keep the existing sort? That's ultimately what should be done in this instance.

Another words, if I click one of the columns and the form reloads, can the existing sort stay some how?
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 »

Yes = change the SQL and add an "ORDER BY" to the SQL.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: Sorting by column in a datagridview

Post by mtartaglia »

Sorry,

I am not being clear enough...

If I click on the 'Name' column to sort after the form loads the first time, then the form reloads (because the timer ran out), how can I keep the sort by 'Name' without doing it in SQL. The reason why I say that is I could have another column sorted, not necessarily the 'Name' column. I hope that was clearer.
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 »

You can also save the sort that is currently defined:

$currentsort = $datagridview1.DataSource.DefaultView.Sort
# reload
$datagridview1.DataSource.DefaultView.Sort = $currentsort
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