Sylloge
A C# helper library
code/Controls/ListViewColumnSorter.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections;
00003 using System.Collections.Generic;
00004 using System.Linq;
00005 using System.Text;
00006 using System.Windows.Forms;
00007 
00008 namespace Sylloge.Controls
00009 {
00010     #region Sorting Class
00011 
00016     public class ListViewColumnSorter : System.Collections.IComparer
00017     {
00018         /* NOTE: How to use this class:
00019          
00020             public class MyClass
00021             {
00022                 ListViewColumnSorter TempColumnSorter = new ListViewColumnSorter();
00023                 ListView TempListView = new ListView();
00024 
00025                 public MyClass() {
00026                     TempListView.ListViewItemSorter = TempColumnSorter;
00027                     TempListView.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(TempListView_ColumnClick);
00028                 }
00029 
00030                 private void TempListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
00031                 {
00032                     if (e.Column == this.FieldColumnSort.SortColumn) {
00033                         // Reverse the current sort direction for this column.
00034                         if (TempColumnSorter.Order == SortOrder.Ascending) {
00035                             TempColumnSorter.Order = SortOrder.Descending;
00036                         } else {
00037                             TempColumnSorter.Order = SortOrder.Ascending;
00038                         }
00039                     } else {
00040                         // Set the column number that is to be sorted; default to ascending.
00041                         TempColumnSorter.SortColumn = e.Column;
00042                         TempColumnSorter.Order = SortOrder.Ascending;
00043                     }
00044                     // Perform the sort with these new sort options.
00045                     TempListView.Sort();
00046                 }
00047             }
00048         */
00049 
00053         private int m_ColumnToSort;
00057         private SortOrder m_OrderOfSort;
00061         private CaseInsensitiveComparer m_ObjectCompare;
00062 
00066         public ListViewColumnSorter()
00067         {
00068             // Initialize the column to '0'
00069             this.m_ColumnToSort = 0;
00070             // Initialize the sort order to 'none'
00071             this.m_OrderOfSort = SortOrder.None;
00072             // Initialize the CaseInsensitiveComparer object
00073             this.m_ObjectCompare = new CaseInsensitiveComparer();
00074         }
00075 
00088         public int Compare(object x, object y)
00089         {
00090             int compareResult;
00091             ListViewItem listviewX, listviewY;
00092             // Cast the objects to be compared to ListViewItem objects
00093             listviewX = (ListViewItem)x;
00094             listviewY = (ListViewItem)y;
00095             // Compare the two items
00096             compareResult = this.m_ObjectCompare.Compare(listviewX.SubItems[this.m_ColumnToSort].Text, listviewY.SubItems[this.m_ColumnToSort].Text);
00097             // Calculate correct return value based on object comparison
00098             if (this.m_OrderOfSort == SortOrder.Ascending) {
00099                 // Ascending sort is selected, return normal result of compare operation
00100                 return compareResult;
00101             } else if (this.m_OrderOfSort == SortOrder.Descending) {
00102                 // Descending sort is selected, return negative result of compare operation
00103                 return (-compareResult);
00104             } else {
00105                 // Return '0' to indicate they are equal
00106                 return 0;
00107             }
00108         }
00109 
00113         public int SortColumn
00114         {
00115             set { this.m_ColumnToSort = value; }
00116             get { return this.m_ColumnToSort; }
00117         }
00118 
00122         public SortOrder Order
00123         {
00124             set { this.m_OrderOfSort = value; }
00125             get { return this.m_OrderOfSort; }
00126         }
00127     }
00128 
00129     #endregion
00130 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events