/// <summary> /// Initializes a new instance of the <see cref="ListViewItemSorter"/> class /// that uses the specified comparers to sort columns /// and attaches the new instance to the specified ListView. /// </summary> /// <param name="listView">The ListView that should be sorted and monitored for column click events.</param> /// <param name="itemComparers">An array of objects that implement IListViewItemComparer. /// The index of the comparer in the array corresponds to the index of the list view column that it sorts. /// If an element is <c>null</c>, the corresponding column cannot be sorted. /// If this parameter is <c>null</c>, all columns can be sorted by their text content.</param> /// <remarks> /// Note that the ListViewItemSorter constructor adds two bitmaps to the /// SmallImageList of the ListView (an empty ImageList will be created /// first if none is assigned). /// </remarks> public ListViewItemSorter(ListView listView, IListViewItemComparer[] itemComparers) { if (listView == null) { throw new ArgumentNullException("listView"); } this.listView = listView; if (itemComparers == null) { IListViewItemComparer defaultComparer = new ListViewTextColumnComparer(); this.itemComparers = new IListViewItemComparer[this.ListView.Columns.Count]; for (int i = 0; i < this.itemComparers.Length; i++) { this.itemComparers[i] = defaultComparer; } } else { this.itemComparers = itemComparers; } if (this.ListView.SmallImageList == null) { this.ListView.SmallImageList = new ImageList(); } this.ListView.SmallImageList.Images.Add("SortAscending", BitmapResources.GetBitmap("Icons.16x16.SortAscending.png")); this.ListView.SmallImageList.Images.Add("SortDescending", BitmapResources.GetBitmap("Icons.16x16.SortDescending.png")); this.ListView.ColumnClick += this.ListViewColumnClick; this.ListView.ListViewItemSorter = this; }
/// <summary> /// Adds another column to compare. /// </summary> /// <param name="comparer">The <see cref="IListViewItemComparer"/> to use to compare this column.</param> /// <param name="column">The 0-based index of the column to compare.</param> public void AddComparer(IListViewItemComparer comparer, int column) { if (comparer == null) { throw new ArgumentNullException("comparer"); } this.comparers.Add(comparer); this.columns.Add(column); }
/// <summary> /// Initializes a new instance of the <see cref="ListViewMultipleColumnsComparer"/> class. /// </summary> /// <param name="firstComparer">The <see cref="IListViewItemComparer"/> to use to compare the first column.</param> /// <param name="firstColumn">The 0-based index of the first column to compare.</param> /// <param name="secondComparer">The <see cref="IListViewItemComparer"/> to use to compare the second column.</param> /// <param name="secondColumn">The 0-based index of the second column to compare.</param> /// <remarks> /// You can add more columns to compare by using the <see cref="AddComparer"/> method. /// </remarks> public ListViewMultipleColumnsComparer(IListViewItemComparer firstComparer, int firstColumn, IListViewItemComparer secondComparer, int secondColumn) { if (firstComparer == null) { throw new ArgumentNullException("firstComparer"); } if (secondComparer == null) { throw new ArgumentNullException("secondComparer"); } this.AddComparer(firstComparer, firstColumn); this.AddComparer(secondComparer, secondColumn); }