{"id":3999,"date":"2012-04-11T09:04:00","date_gmt":"2012-04-11T17:04:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3999"},"modified":"2012-04-18T11:00:54","modified_gmt":"2012-04-18T19:00:54","slug":"spotlight-on-the-listview-control-part-3","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2012\/04\/11\/spotlight-on-the-listview-control-part-3\/","title":{"rendered":"Spotlight on the ListView Control &#8211; Part 3"},"content":{"rendered":"<p><em>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.<\/em><\/p>\n<p>In <a href=\"http:\/\/www.sapien.com\/blog\/2012\/04\/05\/spotlight-on-the-listview-control-part-2\/\">Part 2<\/a> of the Spotlight on the ListView Control, we covered the control&#8217;s properties as well as most of the methods and events. In part 3 we will cover how you can sort items and utilize various helper functions.<\/p>\n<p><em><strong>Sorting ListViewItems:<\/strong><\/em><\/p>\n<p>The ListView supports basic sorting, but it only handles sorting by the first column. In case you want to sort using the content of other columns, you need to write some additional code. Typically you want to sort when a user clicks on a column header, so we will be using the ColumnClick event to trigger sorting.<\/p>\n<p><em>Simple Solution:<\/em><\/p>\n<p>The simplest way to change the sort items is to toggle the Sort property:<\/p>\n<pre><span style=\"color: #8b0000\">$listview1_ColumnClick<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ColumnClickEventHandler]{ \n    <\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ColumnClickEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Sorting <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #ff0000\">'Ascending'<\/span><span style=\"color: #000000\">) \n    { \n        <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Sorting <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #ff0000\">'Descending'<\/span><span style=\"color: #000000\"> \n    } \n}<\/span><\/pre>\n<p>The problem with this simplistic approach is that it doesn\u2019t take into account which column was clicked. In this case, the Sorting property just doesn\u2019t cut it, unless you only have one column.<\/p>\n<p><em>Possible Alternative Solution:<\/em><\/p>\n<p>Next you can try to create you own sort procedure in PowerShell depending on the column clicked.<\/p>\n<p>You can manually sort the items by doing the following:<\/p>\n<pre><span style=\"color: #8b0000\">$listview1_ColumnClick<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ColumnClickEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ColumnClickEventArgs]<\/span><span style=\"color: #000000\">\n\n    <\/span><span style=\"color: #8b0000\">$array<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> System.Collections.ArrayList\n\n    <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items)\n    {\n        <\/span><span style=\"color: #8b0000\">$psobject<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> PSObject \n        <\/span><span style=\"color: #8b0000\">$psobject<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">|<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">Add-Member<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #00008b; font-weight: bold\">type<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'NoteProperty'<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">Name <\/span><span style=\"color: #ff0000\">'Text'<\/span><span style=\"color: #000000\">`\n        <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">value <\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\">.SubItems[<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Column].Text\n        <\/span><span style=\"color: #8b0000\">$psobject<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">|<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">Add-Member<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #00008b; font-weight: bold\">type<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'NoteProperty'<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">Name <\/span><span style=\"color: #ff0000\">'ListItem'<\/span><span style=\"color: #000000\">`\n        <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">value <\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$array<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #8b0000\">$psobject<\/span><span style=\"color: #000000\">)    \n    }\n    \n    <\/span><span style=\"color: #8b0000\">$array<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$array<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">|<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">Sort-Object<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">Property Text)\n    \n    <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.BeginUpdate()\n    \n    <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items.Clear();\n    \n    <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$array<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\">.ListItem)    \n    }\n    \n    <\/span><span style=\"color: #8b0000\">$listview1<\/span><span style=\"color: #000000\">.EndUpdate();\n}<\/span><\/pre>\n<p>Note: When you manually sorting the List, you will want to set the Sorting property to <em>None<\/em>.<\/p>\n<p>In this sample, we are creating custom objects and adding them to a list. We use the custom property to sort our list and after we add the sorted Items back into the ListView. The down side to this method is that you lose any group associations, therefore you need to reassign them. Plus this method isn\u2019t the most efficient way of sorting your items. You are probably better off sorting the original data and rebuilding the ListView again.<\/p>\n<p><em>Final Solution:<\/em><\/p>\n<p>The best solution is to use a ListViewItemSorter class. Unfortunately it requires you to create a custom C# class that will handle the item comparisons. Fortunately for you, we wrote our own custom class to handle the sorting for you. The get around creating a class in PowerShell (something it doesn\u2019t support), we have to use Add-Type cmdlet. The Add-Type cmdlet dynamically compiles C# code and allows you to use the new class directly in PowerShell.<\/p>\n<pre><span style=\"color: #000000\">    <\/span><span style=\"color: #0000ff; font-weight: bold\">Add-Type<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">ReferencedAssemblies (<\/span><span style=\"color: #ff0000\">'System.Windows.Forms'<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">TypeDefinition  @<\/span><span style=\"color: #ff0000\">\" \n    using System;\n    using System.Windows.Forms;\n    using System.Collections;\n    public class ListViewItemComparer : IComparer\n    {\n        public int column;\n        public SortOrder sortOrder;\n        public ListViewItemComparer()\n        {\n            column = 0;\n            sortOrder = SortOrder.Ascending;\n        }\n        public ListViewItemComparer(int column, SortOrder sort)\n        {\n            this.column = column;\n            sortOrder = sort;\n        }\n        public int Compare(object x, object y)\n        {\n            if(column &gt;= ((ListViewItem)x).ListView.Columns.Count ||\n                column &gt;= ((ListViewItem)x).SubItems.Count ||\n                column &gt;= ((ListViewItem)y).SubItems.Count)\n                column = 0;\n        \n            if(sortOrder == SortOrder.Ascending)\n                return String.Compare(((ListViewItem)x).SubItems[column].Text,`<\/span><\/pre>\n<pre><span style=\"color: #ff0000\"> ((ListViewItem)y).SubItems[column].Text);\n            else\n                return String.Compare(((ListViewItem)y).SubItems[column].Text,`<\/span><\/pre>\n<pre><span style=\"color: #ff0000\"> ((ListViewItem)x).SubItems[column].Text);\n        }\n    }\n\"<\/span><span style=\"color: #000000\">@  <\/span><span style=\"color: #0000ff\">|<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">Out-Null<\/span><\/pre>\n<pre><span style=\"color: #0000ff; font-weight: bold\"><\/span>&nbsp;<\/pre>\n<pre><span style=\"color: #0000ff; font-weight: bold\"><\/span><\/pre>\n<pre><span style=\"color: #0000ff; font-weight: bold\"><\/span><\/pre>\n<p>Our custom ListViewItemComparer class will act as a generic sorting class that will allow you to compare any column and choose in which direction to sort.<\/p>\n<p>Next we created a PowerShell function that will allow you to sort the ListView by Column Index:<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Sort-ListViewColumn<\/span><span style=\"color: #000000\"> \n{\n    <\/span><span style=\"color: #008000\">&lt;#\n    .SYNOPSIS\n        Sort the ListView's item using the specified column.\n\n    .DESCRIPTION\n        Sort the ListView's item using the specified column.\n        This function uses Add-Type to define a class that sort the items.\n        The ListView's Tag property is used to keep track of the sorting.\n\n    .PARAMETER ListView\n        The ListView control to sort.\n\n    .PARAMETER ColumnIndex\n        The index of the column to use for sorting.\n        \n    .PARAMETER  SortOrder\n        The direction to sort the items. If not specified or set to None, it will toggle.\n    \n    .EXAMPLE\n        Sort-ListViewColumn -ListView $listview1 -ColumnIndex 0\n#&gt;<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">param<\/span><span style=\"color: #000000\">(    \n            [ValidateNotNull()]\n            [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n            [System.Windows.Forms.ListView]<\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">,\n            [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n            [int]<\/span><span style=\"color: #8b0000\">$ColumnIndex<\/span><span style=\"color: #000000\">,\n            [System.Windows.Forms.SortOrder]<\/span><span style=\"color: #8b0000\">$SortOrder<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'None'<\/span><span style=\"color: #000000\">)\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">((<\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Items.Count <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">0<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000ff\">-or<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$ColumnIndex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-lt<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">0<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000ff\">-or<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$ColumnIndex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ge<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Columns.Count))\n    {\n        <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\">;\n    }\n    \n    <\/span><span style=\"color: #008000\">#region Define ListViewItemComparer<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #0000ff\">try<\/span><span style=\"color: #000000\">{\n        <\/span><span style=\"color: #8b0000\">$local:type<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> [ListViewItemComparer]\n    }\n    <\/span><span style=\"color: #0000ff\">catch<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Add-Type<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">ReferencedAssemblies (<\/span><span style=\"color: #ff0000\">'System.Windows.Forms'<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">TypeDefinition  @<\/span><span style=\"color: #ff0000\">\" \n    using System;\n    using System.Windows.Forms;\n    using System.Collections;\n    public class ListViewItemComparer : IComparer\n    {\n        public int column;\n        public SortOrder sortOrder;\n        public ListViewItemComparer()\n        {\n            column = 0;\n            sortOrder = SortOrder.Ascending;\n        }\n        public ListViewItemComparer(int column, SortOrder sort)\n        {\n            this.column = column;\n            sortOrder = sort;\n        }\n        public int Compare(object x, object y)\n        {\n            if(column &gt;= ((ListViewItem)x).ListView.Columns.Count ||\n                column &gt;= ((ListViewItem)x).SubItems.Count ||\n                column &gt;= ((ListViewItem)y).SubItems.Count)\n                column = 0;\n        \n            if(sortOrder == SortOrder.Ascending)\n                return String.Compare(((ListViewItem)x).SubItems[column].Text,`<\/span><\/pre>\n<pre><span style=\"color: #ff0000\"> ((ListViewItem)y).SubItems[column].Text);\n            else\n                return String.Compare(((ListViewItem)y).SubItems[column].Text,`<\/span><\/pre>\n<pre><span style=\"color: #ff0000\"> ((ListViewItem)x).SubItems[column].Text);\n        }\n    }\n\"<\/span><span style=\"color: #000000\">@  <\/span><span style=\"color: #0000ff\">|<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">Out-Null<\/span><span style=\"color: #000000\">\n    }\n    <\/span><span style=\"color: #008000\">#endregion<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag <\/span><span style=\"color: #0000ff\">-is<\/span><span style=\"color: #000000\"> [ListViewItemComparer])\n    {\n        <\/span><span style=\"color: #008000\">#Toggle the Sort Order<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$SortOrder<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> [System.Windows.Forms.SortOrder]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">None)\n        {\n            <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag.column <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ColumnIndex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-and<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag.sortOrder <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Ascending'<\/span><span style=\"color: #000000\">)\n            {\n                <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag.sortOrder <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Descending'<\/span><span style=\"color: #000000\">\n            }\n            <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n            {\n                <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag.sortOrder <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Ascending'<\/span><span style=\"color: #000000\">\n            }\n        }\n        <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n        {\n            <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag.sortOrder <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$SortOrder<\/span><span style=\"color: #000000\">\n        }\n        \n        <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag.column <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ColumnIndex<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Sort()<\/span><span style=\"color: #008000\">#Sort the items<\/span><span style=\"color: #000000\">\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n    {\n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Sort<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> [System.Windows.Forms.SortOrder]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">None)\n        {\n            <\/span><span style=\"color: #8b0000\">$Sort<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> [System.Windows.Forms.SortOrder]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">Ascending    \n        }\n        \n        <\/span><span style=\"color: #008000\">#Set to Tag because for some reason in PowerShell ListViewItemSorter prop returns null<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> ListViewItemComparer (<\/span><span style=\"color: #8b0000\">$ColumnIndex<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #8b0000\">$SortOrder<\/span><span style=\"color: #000000\">) \n        <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.ListViewItemSorter <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Tag <\/span><span style=\"color: #008000\">#Automatically sorts<\/span><span style=\"color: #000000\">\n    }\n}<\/span><\/pre>\n<p>The first part checks if the ListViewItemComparer types exists. If it doesn\u2019t then it will use the Add-Type cmdlets to add the custom class. Next the functions stores new sorter class in the ListView\u2019s Tag property, so it can track which direction and column it was lasted sorted. This way it can easily toggle between Ascending and Descending when you click the same column.<\/p>\n<p>The last step is to simply call the Sort-ListViewColumn function from ColumnClick event:<\/p>\n<pre><span style=\"color: #8b0000\">$listviewApplications_ColumnClick<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ColumnClickEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ColumnClickEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #008080\">Sort-ListViewColumn<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Column\n}<\/span><\/pre>\n<p>Now you need not worry about creating your own custom sorting script. Just call Sort-ListViewColumn and your done.<\/p>\n<p>Sorted Columns:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Sorted-Column-1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Sorted Column 1\" border=\"0\" alt=\"Sorted Column 1\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Sorted-Column-1_thumb.png\" width=\"244\" height=\"173\"><\/a><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Sorted-Column-2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Sorted Column 2\" border=\"0\" alt=\"Sorted Column 2\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2012\/04\/Sorted-Column-2_thumb.png\" width=\"244\" height=\"173\"><\/a><\/p>\n<p>ListView Help Function:<\/p>\n<p>Here is a help function to add items to the ListView. It will handle the creation of ListViewItems for you.<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Add-ListViewItem<\/span><span style=\"color: #000000\">\n{\n\n    <\/span><span style=\"color: #0000ff\">Param<\/span><span style=\"color: #000000\">( \n    [ValidateNotNull()]\n    [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n    [System.Windows.Forms.ListView]<\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">,\n    [ValidateNotNull()]\n    [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n    <\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">,\n    [int]<\/span><span style=\"color: #8b0000\">$ImageIndex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">1<\/span><span style=\"color: #000000\">,\n    [string[]]<\/span><span style=\"color: #8b0000\">$SubItems<\/span><span style=\"color: #000000\">,\n    [System.Windows.Forms.ListViewGroup]<\/span><span style=\"color: #8b0000\">$Group<\/span><span style=\"color: #000000\">,\n    [<\/span><span style=\"color: #0000ff\">switch<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #8b0000\">$Clear<\/span><span style=\"color: #000000\">)\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Clear<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Items.Clear();\n    }\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-is<\/span><span style=\"color: #000000\"> [Array])\n    {\n        <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">)\n        {        \n            <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\">.ToString(), <\/span><span style=\"color: #8b0000\">$ImageIndex<\/span><span style=\"color: #000000\">)\n            <\/span><span style=\"color: #008000\">#Store the object in the Tag<\/span><span style=\"color: #000000\">\n            <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.Tag <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$item<\/span><span style=\"color: #000000\">\n            \n            <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$SubItems<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n            {\n                <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.SubItems.AddRange(<\/span><span style=\"color: #8b0000\">$SubItems<\/span><span style=\"color: #000000\">)\n            }\n            \n            <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Group<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n            {\n                <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.Group <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Group<\/span><span style=\"color: #000000\">\n            }\n        }\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n    {\n        <\/span><span style=\"color: #008000\">#Add a new item to the ListView<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$ListView<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">.ToString(), <\/span><span style=\"color: #8b0000\">$ImageIndex<\/span><span style=\"color: #000000\">)\n        <\/span><span style=\"color: #008000\">#Store the object in the Tag<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.Tag <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">\n        \n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$SubItems<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n        {\n            <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.SubItems.AddRange(<\/span><span style=\"color: #8b0000\">$SubItems<\/span><span style=\"color: #000000\">)\n        }\n        \n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Group<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n        {\n            <\/span><span style=\"color: #8b0000\">$listitem<\/span><span style=\"color: #000000\">.Group <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Group<\/span><span style=\"color: #000000\">\n        }\n    }\n}<\/span><\/pre>\n<p>Note: These helper functions are already included in PrimalForms 2011 since v2.0.20.<\/p>\n<p>We also created a sample form demonstrates the ListView and column sorting. You can download the <a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PrimalForms 2011\/ListView Sample.zip\">ListView sample<\/a> from our <a href=\"http:\/\/downloads.sapien.com\/\">Downloads<\/a> section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms. In Part 2 of the Spotlight on the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[509,575,644,25],"tags":[641,699,28,633,327],"class_list":["post-3999","post","type-post","status-publish","format-standard","hentry","category-primalforms-software-news","category-primalforms-2011-software-news","category-spotlight-on-controls","category-windows-powershell","tag-controls","tag-listview","tag-powershell","tag-primalforms-2011","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3999"}],"version-history":[{"count":20,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3999\/revisions"}],"predecessor-version":[{"id":4170,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3999\/revisions\/4170"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}