Page 1 of 1

DataGridView + sorted column

Posted: Thu Jul 26, 2012 9:36 pm
by BrunoG
Hello,

I'm trying to sort a column in a dataGridView but i find not the solution.
I load the dataGridView with the function " Load-dateGridView ".

exemple in my code :


[array]$list = Function_listefichier -path $path

Load-dataGridView -DataGridView $main_datagridview -Item list
Here, $list is an array of file with several properties.
The dataGridView is loaded but impossible to sort the columns when i click on the column header. In my code I try :
$main_datagridview.Columns | foreach { $_.SortMode = 'automatic' }

But it does not work.

thank's for your help.

Bruno
FRANCE

DataGridView + sorted column

Posted: Fri Jul 27, 2012 4:55 am
by davidc
The easiest way to do this is to sort the original list and then reload it into the DataGridView. Do to data binding and PowerShell it becomes too complicated to handle it via the Datagridview unless you manually build the grid. David

DataGridView + sorted column

Posted: Thu Aug 16, 2012 10:43 am
by davidc
The latest PowerShell Studio v3.0.8 has a template for sorting the DataGridView. You might want to take a look at it. David

DataGridView + sorted column

Posted: Mon Aug 20, 2012 1:59 am
by BrunoG
Finaly i created in my code an event "ColumnHeaderMouseClick" like this :

$main_datagridview_ColumnHeaderMouseClick=System.Windows.Forms.DataGridViewCellMouseEventHandler]{

if ( ( $GLOBAL_tabSortColonne[$_.ColumnIndex] -eq 'pasTrie' ) -or ( $GLOBAL_tabSortColonne[$_.ColumnIndex] -eq 'descending' ) )
{ $tempTrie=$true ; $global:GLOBAL_tabSortColonne[$_.ColumnIndex] = 'ascending' }
elseif ( $GLOBAL_tabSortColonne[$_.ColumnIndex] -eq 'ascending' )
{ $tempTrie=$false ; $global:GLOBAL_tabSortColonne[$_.ColumnIndex] = 'descending' }

$listResultats = $listResultats | Sort-Object -Property $GLOBAL_tabColonneIndex[$_.ColumnIndex] -Descending:$tempTrie

Load-DataGridView -DataGridView $main_datagridview -Item $listResultats
}
I'm going to install the new version v3.0.8 in order to see the template for sorting the DataGridView.

DataGridView + sorted column

Posted: Mon Aug 20, 2012 4:20 am
by davidc
As long as it works :). The template converts the data into a DataTable, which adds an extra step but it is still worth reviewing.David