• 0

Listview in c# small arrow that appears after sorting


Question

So I finally managed to sort my listview using my own class that inherits the IComparer interface. I know this doesn't look like a big deal but I want to have the small arrow that appears on the column header that you used for sorting.Thanks in advance :happy: .

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

here's the method that is executed when the column header click event occurs

private void listvsort(object sender, ColumnClickEventArgs e)
		{
			procListView.ListViewItemSorter = Sorter;
			Sorter = (ListViewSorter)procListView.ListViewItemSorter;
			if (Sorter.LastSort == e.Column)
				if (procListView.Sorting == SortOrder.Ascending)
					procListView.Sorting = SortOrder.Descending;
				else
					procListView.Sorting = SortOrder.Ascending;
					//procListView.Columns[e.Column].
			else
				procListView.Sorting = SortOrder.Ascending;
			Sorter.ByColumn = e.Column;
			procListView.Sort();
			Sorter.LastSort = e.Column;
		}

and here's the class derived from IComparer,hope it's not too long, I just want the small arrow pointed up or down depending on the sort order just like in windows explorer when you view in details

public class ListViewSorter : IComparer
	{
		int Column = 0;

		public int ByColumn
		{
			get
			{
				return Column;
			}
			set
			{
				Column = value;
			}
		}

		int LastColumn = 0;

		public int LastSort
		{
			get
			{
				return LastColumn;
			}
			set
			{
				LastColumn = value;
			}
		}

		public int Compare(object o1, object o2)
		{
			int result;
			if (!(o1 is ListViewItem))
				return 0;
			if (!(o2 is ListViewItem))
				return 0;
			ListViewItem lvi1 = (ListViewItem) o1;
			ListViewItem lvi2 = (ListViewItem) o2;
			string str1 = lvi1.SubItems[ByColumn].Text;
			string str2 = lvi2.SubItems[ByColumn].Text;
			if (ByColumn != 5)
			{
				if (lvi1.ListView.Sorting == SortOrder.Ascending)
					result = string.Compare(str1, str2);
				else
					result = string.Compare(str2, str1);
			}
			else
			{
				int p1, p2;
				if (str1.CompareTo(Process.Status.Finished.ToString()) == 0) p1 = 5;
				else if (str1.CompareTo(Process.Status.IO.ToString()) == 0) p1 = 4;
				else if (str1.CompareTo(Process.Status.Ready.ToString()) == 0) p1 = 3;
				else if (str1.CompareTo(Process.Status.Finished.ToString()) == 0) p1 = 2;
				else p1 = 1;

				if (str2.CompareTo(Process.Status.Finished.ToString()) == 0) p2 = 5;
				else if (str2.CompareTo(Process.Status.IO.ToString()) == 0) p2 = 4;
				else if (str2.CompareTo(Process.Status.Ready.ToString()) == 0) p2 = 3;
				else if (str2.CompareTo(Process.Status.Finished.ToString()) == 0) p2 = 2;
				else p2 = 1;

				if (lvi1.ListView.Sorting == SortOrder.Ascending)
					result = p1 - p2;
				else
					result = p2 - p1;
			}

			LastSort = ByColumn;
			return result;
		}
	}

tips are also welcomed

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.