Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I accomplished being able to sort on multiple columns, and being able to sort each column as a number, or as text.</p> <p>First use this class:</p> <pre><code>class Sorter : System.Collections.IComparer { public int Column = 0; public System.Windows.Forms.SortOrder Order = SortOrder.Ascending; public int Compare(object x, object y) // IComparer Member { if (!(x is ListViewItem)) return (0); if (!(y is ListViewItem)) return (0); ListViewItem l1 = (ListViewItem)x; ListViewItem l2 = (ListViewItem)y; if (l1.ListView.Columns[Column].Tag == null) { l1.ListView.Columns[Column].Tag = "Text"; } if (l1.ListView.Columns[Column].Tag.ToString() == "Numeric") { float fl1 = float.Parse(l1.SubItems[Column].Text); float fl2 = float.Parse(l2.SubItems[Column].Text); if (Order == SortOrder.Ascending) { return fl1.CompareTo(fl2); } else { return fl2.CompareTo(fl1); } } else { string str1 = l1.SubItems[Column].Text; string str2 = l2.SubItems[Column].Text; if (Order == SortOrder.Ascending) { return str1.CompareTo(str2); } else { return str2.CompareTo(str1); } } } } </code></pre> <p>In your form's constructor, set the sorter like this:</p> <pre><code>lvSeries.ListViewItemSorter = new Sorter(); </code></pre> <p>Then handle the ColumnClick even of your listview control like this:</p> <pre><code>private void lvSeries_ColumnClick(object sender, ColumnClickEventArgs e) { Sorter s = (Sorter)lvSeries.ListViewItemSorter; s.Column = e.Column; if (s.Order == System.Windows.Forms.SortOrder.Ascending) { s.Order = System.Windows.Forms.SortOrder.Descending; } else { s.Order = System.Windows.Forms.SortOrder.Ascending; } lvSeries.Sort(); } </code></pre> <p>This is all dependent on the Tag property of each column either being set to "Numeric" or not, so the sorter knows how to sort.</p> <p>In the above example I cast the values as floats when numeric, you may want to change that to int.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload