C + + Simple implementation MFC ListControl Click Column header sort

Source: Internet
Author: User

Description

SetItemData can bind a variable of type DWORD for each row. This variable can be obtained with getitemdata.
For example, suppose you need to display a record in a data table in CListCtrl, which has a serial number primary key ID, which in general does not mean much to the user, so you do not need to display it in the visible column of CListCtrl. But often do specific queries and other operations, you need to use this ID to complete. At this time, with SetItemData to bind it to each row, will be very convenient, user operation which line, then use GetItemData can get the corresponding record ID, directly used to do the operation, very cool.
Because the binding is a DWORD type, there is also an extension that binds a pointer object. For example, here I am no longer an ID primary key, but need to associate a data structure, then you can bind the pointer of the data structure object SetItemData. In doing so, you can omit the process of finding a bunch of data structures in an array.

The first step:

To define a global variable:

int sort_column; Record the clicked column

bool method; Record comparison method

Step Two:

Add a comparison function

//comparison function2staticint CALLBACK Mycompareproc (LPARAM lParam1, LPARAM lParam2, LPARAM lparamsort) {//extract two rows of data from a parameter that you want to compare LCintRow1 = (int) lParam1;intRow2 = (int) lParam2; CListCtrl* LC = (clistctrl*) Lparamsort; CString LP1= lc->GetItemText (Row1,sort_column); CString LP2= lc->GetItemText (row2,sort_column);//Compare, for different columns, different comparisons, note the previous sort direction, next to the reverse sort if(sort_column<2){//comparison of type int if(method)returnAtoi (LP1)-atoi (LP2);Else returnAtoi (LP1)-atoi (LP1);}Else{//Comparison of text types if(method)returnlp1.comparenocase (LP2); Else returnlp2.comparenocase (LP1); } Return0; }

Step three: Add column header click event Lvn_columnclick

voidClistdlg::onlvncolumnclickxxx (NMHDR *pnmhdr, LRESULT *PResult) {Lpnmlistview PNMLV= reinterpret_cast<lpnmlistview>(PNMHDR);//TODO: Add control notification handler code hereSort_column = pnmlv->isubitem;//Click on the columnintCount =M_list_port. GetItemCount (); for(intI=0; i<count;i++) M_list_port. SetItemData (I,i); //The comparison keyword for each line, which is the column ordinal (clicked column number), can be set to the 12th parameter of the other comparison functionM_list_port. Sortitems (Mycompareproc, (dword_ptr) &m_list_port);//sort the second argument is the third parameter of the comparison function*presult =0;}

Complete.

method is used to control the order of orders, see Personal settings.

C + + Simple implementation MFC ListControl Click Column header sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.