Datagridview Sort Columns

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
User avatar
wallen
Posts: 18
Joined: Thu Feb 09, 2012 10:10 am

Datagridview Sort Columns

Post by wallen » Wed Mar 21, 2012 4:03 am

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 )

User avatar
lbrown
Posts: 27
Joined: Fri Feb 18, 2011 6:02 am

Datagridview Sort Columns

Post by lbrown » Wed Mar 21, 2012 4:51 am

This is something that I am very interested in as well..

User avatar
wallen
Posts: 18
Joined: Thu Feb 09, 2012 10:10 am

Datagridview Sort Columns

Post by wallen » Wed Mar 21, 2012 6:50 am

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

User avatar
Alexander Riedel
Posts: 6861
Joined: Tue May 29, 2007 4:43 pm

Datagridview Sort Columns

Post by Alexander Riedel » Wed Mar 21, 2012 8:42 am

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.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
wallen
Posts: 18
Joined: Thu Feb 09, 2012 10:10 am

Datagridview Sort Columns

Post by wallen » Wed Mar 21, 2012 9:03 am

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!

User avatar
Alexander Riedel
Posts: 6861
Joined: Tue May 29, 2007 4:43 pm

Datagridview Sort Columns

Post by Alexander Riedel » Wed Mar 21, 2012 9:13 am

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
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
wallen
Posts: 18
Joined: Thu Feb 09, 2012 10:10 am

Datagridview Sort Columns

Post by wallen » Wed Mar 21, 2012 9:19 am

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

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

Datagridview Sort Columns

Post by davidc » Mon Mar 26, 2012 5:47 am

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
David
SAPIEN Technologies, Inc.

User avatar
lbrown
Posts: 27
Joined: Fri Feb 18, 2011 6:02 am

Datagridview Sort Columns

Post by lbrown » Tue Mar 27, 2012 7:55 am

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...

User avatar
wallen
Posts: 18
Joined: Thu Feb 09, 2012 10:10 am

Datagridview Sort Columns

Post by wallen » Wed Mar 28, 2012 6:33 am



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."