Datagridview Sort Columns

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 12 years 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.
User avatar
wallen
Posts: 18
Last visit: Fri Mar 15, 2013 9:36 am

Datagridview Sort Columns

Post 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 )
User avatar
lbrown
Posts: 27
Last visit: Tue Mar 19, 2013 12:03 pm

Datagridview Sort Columns

Post by lbrown »

This is something that I am very interested in as well..
User avatar
wallen
Posts: 18
Last visit: Fri Mar 15, 2013 9:36 am

Datagridview Sort Columns

Post 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
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Datagridview Sort Columns

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
wallen
Posts: 18
Last visit: Fri Mar 15, 2013 9:36 am

Datagridview Sort Columns

Post 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!
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Datagridview Sort Columns

Post 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
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
wallen
Posts: 18
Last visit: Fri Mar 15, 2013 9:36 am

Datagridview Sort Columns

Post 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
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Datagridview Sort Columns

Post 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
David
SAPIEN Technologies, Inc.
User avatar
lbrown
Posts: 27
Last visit: Tue Mar 19, 2013 12:03 pm

Datagridview Sort Columns

Post 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...
User avatar
wallen
Posts: 18
Last visit: Fri Mar 15, 2013 9:36 am

Datagridview Sort Columns

Post 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."
This topic is 12 years 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.