Solved

Sort Column using CListCtl

Posted on 1998-02-12
3
218 Views
Last Modified: 2008-02-01
I'm using MSVC++4.1.  I have a CListCtl (Report View) class and I can't figure out how to use the SortItems() function with the Callback routine if the user were to click on a column. I was wondering if I could get some coding examples on how to do this.
0
Comment
Question by:kingservant
3 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1181893
Look at "codeguru" site at http://www.codeguru.com/listview/listview_toc.shtml. There are several article about sorting of list view control
0
 

Author Comment

by:kingservant
ID: 1181894
The examples on that web site didn't quite have the solution I needed.  I guess what I am looking for is examples on using the CListCtrl column click message to invoke my sort using SortItems().
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 80 total points
ID: 1181895
Here is an excerpt from one of my programs:

void CGuidListCtrl::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = reinterpret_cast<NM_LISTVIEW*>(pNMHDR);
      int curSortCol = pNMListView->iSubItem;

      OnColumnClickImp(curSortCol);

      SortFields();
      *pResult = 0;
}

void CGuidListCtrl::SortFields()
{
      CWaitCursor wait;
      SortItems(SortCB, reinterpret_cast<LPARAM>(this));
}

int CALLBACK CGuidListCtrl::SortCB(LPARAM item1, LPARAM item2, LPARAM itemData)
{
      CGuidListCtrl* This = reinterpret_cast<CGuidListCtrl*>(itemData);

      int itemIndex1 = This->GetListIndexOfItemData(item1);
      ASSERT(itemIndex1 != -1);
      CString text1 = This->GetItemText(itemIndex1, This->GetSortColumn());

      int itemIndex2 = This->GetListIndexOfItemData(item2);
      ASSERT(itemIndex2 != -1);
      CString text2 = This->GetItemText(itemIndex2, This->GetSortColumn());

      if (This->IsSortAscending())
            return text1.CompareNoCase(text2);
      else
            return text2.CompareNoCase(text1);

      return 0;
}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now