[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.
This topic is 6 years and 1 month 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
GHIsolierung
Posts: 62
Last visit: Tue Nov 02, 2021 12:46 am

[solved]Sort Datagridview when using DataTable

Post by GHIsolierung »

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 GHIsolierung on Tue Feb 13, 2018 8:34 am, edited 1 time in total.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Sort Datagridview when using DataTable

Post by davidc »

[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
GHIsolierung
Posts: 62
Last visit: Tue Nov 02, 2021 12:46 am

Re: Sort Datagridview when using DataTable

Post by GHIsolierung »

puhh...I was so blind...

thanks
This topic is 6 years and 1 month 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