Page 1 of 1

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 4:03 am
by wallen
I can't seem to find the SortMode property for the DataGridView form. So, my next guess was to use the "ColumnHeaderMouseClick" and assign it to a script block for sorting. Unforunately the examples on MSDN for the Sort Method are only in C# aned VB: http://technet.microsoft.com/en-us/libr ... 8ft3z.aspx So, not being a programmer... I'm not sure how to translate these examples into powershell code. Basically, I have 2 columns in a datagridview. I'm trying to get the columns to sort by clicking header coumn 1 (toggles acsending / decending by column 1 data) or by clicking column 2 toggles acsending / decending by column 2 data) A nudge in the right direction would be appreciated. Thanks. (PS. Still waiting for the spotlight on the datagridview control on your blog... sorting columns and jumping to a cell by typing the first few letters would be some cool tricks to include! - Thanks )

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 4:51 am
by lbrown
This is something that I am very interested in as well..

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 6:50 am
by wallen
This is what I got so far: ----------$SortToggle = 0$SortColumn_Click={ if (($SortToggle % 2) -eq 0){ $datagridviewResults.Sort($datagridviewResults.Columns[e.ColumnIndex], ListSortDirection.Ascending) } else{ $datagridviewResults.Sort($datagridviewResults.Columns[e.ColumnIndex], ListSortDirection.Descending) $SortToggle++ }}---------------I stole this from here:http://www.c-sharpcorner.com/uploadfile ... perations/ Which I believe was in C++The problem is the [e.ColumnIndex]... I'm not sure what PrimalForms is sending the parameter from the event-property "ColumnHeaderMouseClick" back as. Obviously, it ain't "e".
wallen2012-03-21 13:51:12

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 8:42 am
by Alexander Riedel
e is just a name. It's the the event parameter.

In PowerShell a C# line like
DataGridViewColumn oldColumn = dataGridView1.SortedColumn;

translates to
[DataGridViewColumn] $oldColumn = $dataGridView1.SortedColumn

Note that the types are expressed differently and variables, unlike in PowerShell, have no designating character like '$'.

Using properties and methods follow the same notation, <object>.property or <object>.method()

You have a form variable for your control declared within PrimalForms, so that is what you use for <object>.

In an event handler $_ is the underlying event parameter, so

[DataGridViewCellMouseEventArgs]$_.ColumnIndex

or so should get you what you need.

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 9:03 am
by wallen
This:---------$SortColumn_Click={ if (($SortToggle % 2) -eq 0){ $datagridviewResults.Sort([DataGridViewCellMouseEventArgs]$_.ColumnIndex, ListSortDirection.Ascending) } else{ $datagridviewResults.Sort([DataGridViewCellMouseEventArgs]$_.ColumnIndex, ListSortDirection.Descending) $SortToggle++ }}---------- Doesn't work either.$DatagridviewResults is the variable name of the form.The Event "ColumnHeaderMouseClick" is assigned to the scriptblock $SortColumn_Click.I'm calling the method sort(), which requires the columnindex and sort order as paremeters. Like you said: <object>.method() - which in my case should be $DatagridviewResults.sort(), right? or am I way off base? Thanks for the replies!

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 9:13 am
by Alexander Riedel
I don't know what you named your variables :-)

Why don't you try with a simple example:

$datagridviewResults.Sort(0, ListSortDirection.Descending)

To sort on column 0 (or any number you choose).

There may be more casting involved here, but the resident PowerShell forms expert isn't back until Monday.

Alexander Riedel2012-03-21 16:15:53

Datagridview Sort Columns

Posted: Wed Mar 21, 2012 9:19 am
by wallen
I'll try that, but I really don't have a variable defined... I was expecting some pre-defined variable coming from the event trigger (which I was hoping was "e" as some sort of standard).
Edit: Here is the output:>> Platform: 32Bit (STA)
ERROR: Missing expression after ','.
NVDOT.pff (51): ERROR: At Line: 51 char: 31
ERROR: + $datagridviewResults.Sort(0, <<<< ListSortDirection.Ascending)
ERROR: + CategoryInfo : ParserError: (,:String) [], ParseException
ERROR: + FullyQualifiedErrorId : MissingExpressionAfterToken
ERROR:

>> Execution time: < 1 second
>> Script Endedwallen2012-03-21 16:22:30

Datagridview Sort Columns

Posted: Mon Mar 26, 2012 5:47 am
by davidc
Try the following: $datagridviewResults.Sort($datagridviewResults.Columns[$_.ColumnIndex],
'Ascending')Note: If you are using DataSource Property, the sorting only works with certain types, such as a DataTable from a database query.If you manually add the rows to the grid, then you will need to create a SortCompare event handler. Unfortunately sorting rows in a DataGridView is not trivial. David

Datagridview Sort Columns

Posted: Tue Mar 27, 2012 7:55 am
by lbrown
I would love to see a basic scenario like: Get Service information from a computerSort depending on the column header clicked. That would bring it all together...

Datagridview Sort Columns

Posted: Wed Mar 28, 2012 6:33 am
by wallen


Try the following:

$datagridviewResults.Sort($datagridviewResults.Columns[$_.ColumnIndex],
'Ascending')

Note: If you are using DataSource Property, the sorting only works with certain types, such as a DataTable from a database query.If you manually add the rows to the grid, then you will need to create a SortCompare event handler. Unfortunately sorting rows in a DataGridView is not trivial. DavidWell, I guess I am manually adding the rows because the grid is created from a CSV file. I get the following error when clicking the header: ERROR: Exception calling "Sort" with "2" argument(s): "DataGridView control must be bound to an IBindingList object to be sorted."