Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 3938 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!