PowerShell Studio 2014 - DataGridView Sorting

When creating graphical tools with PowerShell Studio 2014 and using the DataGridView control, I often find myself looking for the following tip!

The DataGridView control provides a customizable table for displaying data. You can extend the DataGridView control in a number of ways to build custom behaviors into your applications. A DataView provides a means to filter and sort data within a DataTable. The following example shows how to sort a DataGridView by using a DataTable Object.

In this example, I'm using PowerShell Studio 2014 from SAPIEN, you can download a 45 days trial version here.

How to sort DataGridView ?

Using PowerShell Studio 2014, let's create a simple GUI and add a DataGridView control

Add a DataGridView

For test purpose, I will add some PowerShell code to load the list of processes currently running and the machine and send it to the DataGridView control:

$form1_Load = {
    $Processes = Get-Process | Select-Object -Property Name, id, ws
    Load-DataGridView -DataGridView $datagridview1 -Item $Processes

If you try to launch the tool, you'll see the following. At this point we can't sort anything yet.

Basic GUI

Converting to a Database object

One of the requirement to allow the sorting feature is to convert the PowerShell Object to a [System.Data.Datatable] object.

Function ConvertTo-DataTable
Thanks the guys at SAPIEN, they created a small function to do this: ConvertTo-DataTable

To load this function, open the snippets using the CTRL+K shortcut and select "Convert To Database"

ConvertTo-Datatable preview
You can find the ConvertTo-Datatable here.

Convert PSObject to DataTable
Now we need to convert the output of Get-Process to a DataTable object:

$form1_Load = {
    $Processes = Get-Process | Select-Object -Property Name, id, ws
    $ProcessesDT = ConvertTo-DataTable -InputObject $Processes
    Load-DataGridView -DataGridView $datagridview1 -Item $ProcessesDT

Add an event ColumnHeaderMouseClick

Next, we need to add an event when the user click on one of the Column Header
In the designer view, select your DataGridView and double click on the event called: ColumnHeaderMouseClick

ColumnHeaderMouseClick event (DataGridView Control)

Finally add this piece of code, to enable the feature:

#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
    if ($datagridview1.DataSource -is [System.Data.DataTable])
        $column = $datagridview1.Columns[$_.ColumnIndex]
        $direction = [System.ComponentModel.ListSortDirection]::Ascending
        if ($column.HeaderCell.SortGlyphDirection -eq 'Descending')
            $direction = [System.ComponentModel.ListSortDirection]::Descending
        $datagridview1.Sort($datagridview1.Columns[$_.ColumnIndex], $direction)

Sorting in action

Now let's run the tool again and click on "Name" a couple of times for example, It will sort on this property.

Code Example

Finally, the code is available on GitHub or Technet Gallery

Thanks for reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter @lazywinadm / Google+ / LinkedIn. You can also follow the LazyWinAdmin Blog on Facebook Page and Google+ Page.

No comments:

Post a Comment