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)
}
}
Sorting by column in a datagridview
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.
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.
Re: Sorting by column in a datagridview
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.
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.
- mtartaglia
- Posts: 101
- Last visit: Mon Dec 19, 2022 11:45 am
Re: Sorting by column in a datagridview
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.
- mtartaglia
- Posts: 101
- Last visit: Mon Dec 19, 2022 11:45 am
Re: Sorting by column in a datagridview
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.
Re: Sorting by column in a datagridview
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.
You an also just add an "ORDER BY" to your SQL and the table will be sorted when it is retrieved.
- mtartaglia
- Posts: 101
- Last visit: Mon Dec 19, 2022 11:45 am
Re: Sorting by column in a datagridview
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?
Another words, if I click one of the columns and the form reloads, can the existing sort stay some how?
- mtartaglia
- Posts: 101
- Last visit: Mon Dec 19, 2022 11:45 am
Re: Sorting by column in a datagridview
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.
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.
Re: Sorting by column in a datagridview
You can also save the sort that is currently defined:
$currentsort = $datagridview1.DataSource.DefaultView.Sort
# reload
$datagridview1.DataSource.DefaultView.Sort = $currentsort
$currentsort = $datagridview1.DataSource.DefaultView.Sort
# reload
$datagridview1.DataSource.DefaultView.Sort = $currentsort