Sign in to follow this  

[.net] Sorting listView items by Column click WITH each column support toggle ASC/DESC sorti

This topic is 3633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to ask how can I handle Sorting listView items by Column click? I have successfully implement this one: This one manage listView1_ColumnClick event: private: System::Void listView1_ColumnClick(System:bject^ sender, System::Windows::Forms::ColumnClickEventArgs^ e) { // Determine whether the column is the same as the last column clicked. if (e->Column != sortColumn) { // Set the sort column to the new column. sortColumn = e->Column; // Set the sort order to ascending by default. if (listView1->Sorting == SortOrder::Ascending) listView1->Sorting = SortOrder:escending; else listView1->Sorting = SortOrder::Ascending; //listView1->Sorting = SortOrder::Ascending; } else { // Determine what the last sort order was and change it. if (listView1->Sorting == SortOrder::Ascending) listView1->Sorting = SortOrder:escending; else listView1->Sorting = SortOrder::Ascending; } // Call the sort method to manually sort. listView1->Sort(); // Set the ListViewItemSorter property to a new ListViewItemComparer // object. this->listView1->ListViewItemSorter = gcnew ListViewItemComparer(e->Column, listView1->Sorting); } //listView1_ColumnClick }; This one manage the class ListViewItemComparer: ref class ListViewItemComparer : System::Collections::IComparer { private: int col; private: SortOrder^ order; public: ListViewItemComparer() { col = 0; order = SortOrder::Ascending; } public: ListViewItemComparer(int column, SortOrder^ order) { col = column; this->order = order; } public: virtual int Compare(Object^ x, Object^ y) { int returnVal= -1; returnVal = String::Compare(((ListViewItem^)x)->SubItems[col]->Text, ((ListViewItem^)y)->SubItems[col]->Text); // Determine whether the sort order is descending. if(order == SortOrder:escending) // Invert the value returned by String.Compare. returnVal *= -1; return returnVal; } }; it is working but what I want is like the Windows Explorer, so if I click a column it will be sorted either Ascending or Descending and if I click that same column again, it will be sorted reversely. What have I done above CANNOT do this. How to do this? If you don't mind, please fix my codes above. Thank you very much.

Share this post


Link to post
Share on other sites

This topic is 3633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this