Click here to Skip to main content
14,825,425 members
Articles / Desktop Programming / MFC
Posted 8 Jun 2005


44 bookmarked

An effective way to hide or show columns in list control dynamically

Rate me:
Please Sign up or sign in to vote.
4.62/5 (14 votes)
8 Jun 2005CPOL1 min read
An article on how to dynamically hide columns in a list control.

CListCtrlEx Demo


Recently, in one of my projects, I needed a CListCtrl derived class which has the ability to show/hide its columns dynamically. Using CListCtrl::DeleteColumn() is not a good idea because in that case the index of the sub items will be changed from time to time, thus assessing the sub items will become very complicated. I searched the Internet and did not find an existing solution for this. At last, I had to write my own class - CListCtrlEx to implement the feature.

The main features of CListCtrlEx

  • Dynamically hide/show the columns.
  • Provide a built-in context menu to select the columns and it is dynamically created based on the columns you insert.
  • Drag-and-drop reorder columns (you must enable the LVS_EX_HEADERDRAGDROP style).
  • Save/restore the columns' order, width and visibility in/from registry automatically.
  • You do not need to change your existing code of CListCtrl derived classes. All you have to do is change their base class to CListCtrlEx.

Using the CListCtrlEx class

  1. Add the following files to your project:
    • HeaderCtrlEx.h
    • HeaderCtrlEx.cpp
    • ListCtrlEx.h
    • ListCtrlEx.cpp
  2. Import the following two cursor files into you resource and change their ID as mentioned in the square brackets:
  3. Derive your own class, say CMyListCtrl, from CListCtrlEx.
  4. Add a list control to your dialog in the resource editor and bind it to a member variable, say m_wndList. Of course the variable should be a CMyListCtrl object.
    void CTestListCtrlDlg::DoDataExchange(CDataExchange* pDX)
        DDX_Control(pDX, IDC_LIST, m_wndList);
  5. Save/restore the state of the columns.
    BOOL CTestListCtrlDlg::OnInitDialog()
        // Enable column drag & drop
                                      | LVS_EX_HEADERDRAGDROP);
        // Insert columns and items to the list control here
        // Set the sub registry key to store the columns' state
        // Restore the state of the columns
    void CTestListCtrlDlg::OnDestroy()
        // TODO: Add your message handler code here
        // Save the state of the columns


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chen Hao
Web Developer
China China
Chen Hao is a programmer of SourceTec Software Co., LTD.
He began to program since 1992 and programming became
one of the most important things in his life since then.

His most recent projects were Sothink SWF Quicker and
Sothink SWF Decompiler.

Comments and Discussions

QuestionChange font of a menu? Pin
bosfan4-Oct-13 1:43
Memberbosfan4-Oct-13 1:43 
GeneralFix in DrawDragDivider() Pin
Thornets19-Dec-07 23:35
MemberThornets19-Dec-07 23:35 
Questionmultiple selection in menu Pin
sharathgowda10-Jul-07 2:18
Membersharathgowda10-Jul-07 2:18 
GeneralAn issue though! Pin
Nibu babu thomas30-May-07 22:59
MemberNibu babu thomas30-May-07 22:59 
GeneralPort to VC6! Pin
cristitomi5-Apr-07 1:33
Membercristitomi5-Apr-07 1:33 
GeneralRe: Port to VC6! Pin
Leon Salters14-May-07 22:13
MemberLeon Salters14-May-07 22:13 
GeneralRe: Port to VC6! Pin
cristitomi17-May-07 5:03
Membercristitomi17-May-07 5:03 
QuestionRe: Port to VC6! Pin
Leon Salters4-Jun-07 1:09
MemberLeon Salters4-Jun-07 1:09 
GeneralBug with column order Pin
Paddy47830-Sep-06 1:29
MemberPaddy47830-Sep-06 1:29 
QuestionProblems to include functionalities to my project [modified] Pin
crisoc22-May-06 5:08
Membercrisoc22-May-06 5:08 
AnswerRe: Problems to include functionalities to my project Pin
Floppe11-Jul-06 2:04
MemberFloppe11-Jul-06 2:04 
GeneralRe: Problems to include functionalities to my project Pin
crisoc11-Jul-06 2:20
Membercrisoc11-Jul-06 2:20 
GeneralASSERT during creation within CView derived class Pin
KeesR14-May-06 4:52
MemberKeesR14-May-06 4:52 
GeneralWidth bug (with solution) Pin
KeesR14-May-06 4:45
MemberKeesR14-May-06 4:45 
Questionguidance needed to implement this feature for a Listview Pin
jaisri.anni7-May-06 22:22
Memberjaisri.anni7-May-06 22:22 
GeneralFix needed in header control Pin
Hogne Fossland5-Mar-06 21:56
MemberHogne Fossland5-Mar-06 21:56 
Questioneasier way? Pin
vfilll2-Oct-05 6:14
Membervfilll2-Oct-05 6:14 
AnswerRe: easier way? Pin
Helen_Bakulina3-May-06 5:20
MemberHelen_Bakulina3-May-06 5:20 
AnswerRe: easier way? Pin
David Crow11-May-07 2:49
MemberDavid Crow11-May-07 2:49 
GeneralCListCtrlEx Pin
phoenixwu27-Jun-05 3:41
Memberphoenixwu27-Jun-05 3:41 
GeneralOT: You might think about replacing that image... Pin
Johann Gerell9-Jun-05 23:43
MemberJohann Gerell9-Jun-05 23:43 
GeneralNice Pin
the-unforgiven8-Jun-05 21:37
Memberthe-unforgiven8-Jun-05 21:37 
GeneralRe: Nice Pin
Alexander M.,9-Jun-05 5:42
MemberAlexander M.,9-Jun-05 5:42 
GeneralRe: Nice Pin
MMs_xH30-Jun-05 2:40
MemberMMs_xH30-Jun-05 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.