[solved]Sort Datagridview when using DataTable

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.
Locked
User avatar
rubberduck
Posts: 48
Joined: Wed Oct 30, 2013 9:34 am

[solved]Sort Datagridview when using DataTable

Post by rubberduck » Tue Feb 13, 2018 2:13 am

Sapien Powershell Studio 2017 v 5.4.141, Windows 10 x64.

I know that the topic has already been discussed several times and I have read a few posts in this forum on the topic, but I still don't get smarter and need help.

I have to sort the individual columns of a datagrid view and have read that this is only possible if the datagrid view is a DataTable.

This is what I do.
1. I have a function that reads out AD computers and then creates a new PSCustomobject.
2. I convert this object afterwards with' Convertto-DataTable'.
3. then I fill the datagridview with' Update-DataGridview'.

Code: Select all

$btnADComputer_Click={
	
	$GHComputer = Get-GHComputer
	
	$Datatable = ConvertTo-DataTable -InputObject $GHComputer 
	
	Update-DataGridView -DataGridView $datagridview -Item $GHComputer -AutoSizeColumns AllCells
	

}
It works the way it's supposed to.

I want to use a 'ColumnHeaderMouseDoubleClick' event for sorting.
(http://www.lazywinadmin.com/2015/01/pow ... dview.html)

Code: Select all

$datagridview_ColumnHeaderMouseDoubleClick=[System.Windows.Forms.DataGridViewCellMouseEventHandler]{
	
#	[System.Windows.Forms.MessageBox]::Show("Hallo") ##DEBUG

		$column = $datagridview.Columns[$_.ColumnIndex]
		$direction = [System.ComponentModel.ListSortDirection]::Ascending
		
		if ($column.HeaderCell.SortGlyphDirection -eq 'Descending')
		{
			$direction = [System.ComponentModel.ListSortDirection]::Descending
		}
		
		$datagridview.Sort($datagridview.Columns[$_.ColumnIndex], $direction)
	
}
( Any other Method is welcome too, this is what I found so far )


Unfortunately I get the error message :
"DataGridView control must be bound to an IBindingList object to be sorted."
Of course, I found a lot of information about this on the internet, but this is where my understanding ends.
Apparently I have to save the datagridview again, bind it to a BindingList, sort it and print it again....

I need help with the implementation using an example, if possible.

Thank you very much for your help
Last edited by rubberduck on Tue Feb 13, 2018 9:34 am, edited 1 time in total.

User avatar
davidc
Posts: 5341
Joined: Thu Aug 18, 2011 4:56 am

Re: Sort Datagridview when using DataTable

Post by davidc » Tue Feb 13, 2018 8:59 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

Pass the $Datatable variable to Update-DataGridView instead of $GHComputer.

Code: Select all

Update-DataGridView -DataGridView $datagridview -Item $Datatable -AutoSizeColumns AllCells
David
SAPIEN Technologies, Inc.

User avatar
rubberduck
Posts: 48
Joined: Wed Oct 30, 2013 9:34 am

Re: Sort Datagridview when using DataTable

Post by rubberduck » Tue Feb 13, 2018 9:33 am

puhh...I was so blind...

thanks

Locked