Answered by:
$DataGridView.Sort() - Sort by 2 columns

Question
-
I am able to use $DataGridView.Sort($DataGridView.Columns[3],'Descending') to sort by Column 3, thanks for Jrv's help. Is there a way also to sort by another column after sorted by Column 3? Thanks.
- Edited by Ian3 Friday, November 1, 2019 2:36 PM
Friday, November 1, 2019 2:35 PM
Answers
-
As I noted above you have to us4e a DataTable to sort by multiple columns. Put the data in a DataTable and bind it to the DataSource property then you can set the DefaultView.Sort of the table to sort on multiple columns.
$table.DefaultView.Sort = "Name DESC, age ASC'
\_(ツ)_/
Saturday, November 2, 2019 1:49 PM -
The easy way to build a quick DataTable.
You can also do this in a loop from a value array or a CSV,
$table = [system.Data.DataTable]::New() $cols = $table.Columns.AddRange(@('TIme','Name','Age')) $table.Rows.Add(@([datetime]::Now.ToString('HH:mm:ss'),'John',12)) $table.Rows.Add(@([datetime]::Now.ToString('HH:mm:ss'),'Bill',12)) $table.Rows.Add(@([datetime]::Now.ToString('HH:mm:ss'),'Alice',12))
\_(ツ)_/
- Marked as answer by Ian3 Saturday, November 2, 2019 2:13 PM
Saturday, November 2, 2019 2:09 PM
All replies
-
This can be done by using a DataTable as the grids DataSource and setting the sort on the DefaultView.
\_(ツ)_/
Friday, November 1, 2019 6:03 PM -
I created the code that sorts by Name (Ascending). How to sort Name Descending and Age Ascending? Thanks
$table = New-Object system.Data.DataTable
$col0 = New-Object system.Data.DataColumn Time,([string])
$col1 = New-Object system.Data.DataColumn Name,([string])
$col2 = New-Object system.Data.DataColumn Age,([string])
$table.columns.add($col0)
$table.columns.add($col1)
$table.columns.add($col2)
$row0 = $table.NewRow()
$row1 = $table.NewRow()
$row2 = $table.NewRow()
$row0.Time = (Get-Date).ToString('HH:mm:ss')
$row0.Name = "John"
$row0.Age = "33"
$table.Rows.Add($row0)
$row1.Time = (Get-Date).ToString('HH:mm:ss')
$row1.Name = "Bill"
$row1.Age = "54"
$table.Rows.Add($row1)
$row2.Time = (Get-Date).ToString('HH:mm:ss')
$row2.Name = "John"
$row2.Age = "28"
$table.Rows.Add($row2)$form = New-Object System.Windows.Forms.Form
$form.Size = '745,585'
$form.StartPosition = 'Manual'
$form.Location = '500, 200'$dataGridView= New-Object System.Windows.Forms.DataGridView
$dataGridView.Size = '720,485'
$dataGridView.Location = '5, 25'
$dataGridView.DataSource = $table
$table.DefaultView.Sort = 'Name'$form.Controls.Add($DataGridView)
$form.ShowDialog() | Out-NullSaturday, November 2, 2019 1:35 PM -
As I noted above you have to us4e a DataTable to sort by multiple columns. Put the data in a DataTable and bind it to the DataSource property then you can set the DefaultView.Sort of the table to sort on multiple columns.
$table.DefaultView.Sort = "Name DESC, age ASC'
\_(ツ)_/
Saturday, November 2, 2019 1:49 PM -
The easy way to build a quick DataTable.
You can also do this in a loop from a value array or a CSV,
$table = [system.Data.DataTable]::New() $cols = $table.Columns.AddRange(@('TIme','Name','Age')) $table.Rows.Add(@([datetime]::Now.ToString('HH:mm:ss'),'John',12)) $table.Rows.Add(@([datetime]::Now.ToString('HH:mm:ss'),'Bill',12)) $table.Rows.Add(@([datetime]::Now.ToString('HH:mm:ss'),'Alice',12))
\_(ツ)_/
- Marked as answer by Ian3 Saturday, November 2, 2019 2:13 PM
Saturday, November 2, 2019 2:09 PM -
You can also do this for non-string columns.
$table = [system.Data.DataTable]::New() $col = [System.Data.DataColumn]::New('Age',[uint16]) $table.Columns.AddRange(@('Time','Name',$col))
FOr numbers to sort correctly they need to be ints and not strings.
\_(ツ)_/
- Edited by jrv Saturday, November 2, 2019 2:31 PM
Saturday, November 2, 2019 2:30 PM