Click here to Skip to main content
Click here to Skip to main content

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

, 8 Jun 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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? Pinmemberbosfan4-Oct-13 2:43 
GeneralFix in DrawDragDivider() PinmemberThornets20-Dec-07 0:35 
Questionmultiple selection in menu Pinmembersharathgowda10-Jul-07 3:18 
GeneralAn issue though! PinmemberNibu babu thomas30-May-07 23:59 
GeneralPort to VC6! Pinmembercristitomi5-Apr-07 2:33 
GeneralRe: Port to VC6! PinmemberLeon Salters14-May-07 23:13 
GeneralRe: Port to VC6! Pinmembercristitomi17-May-07 6:03 
QuestionRe: Port to VC6! PinmemberLeon Salters4-Jun-07 2:09 
GeneralBug with column order PinmemberPaddy47830-Sep-06 2:29 
QuestionProblems to include functionalities to my project [modified] Pinmembercrisoc22-May-06 6:08 
AnswerRe: Problems to include functionalities to my project PinmemberFloppe11-Jul-06 3:04 
GeneralRe: Problems to include functionalities to my project Pinmembercrisoc11-Jul-06 3:20 
GeneralASSERT during creation within CView derived class PinmemberKeesR14-May-06 5:52 
GeneralWidth bug (with solution) PinmemberKeesR14-May-06 5:45 
Questionguidance needed to implement this feature for a Listview Pinmemberjaisri.anni7-May-06 23:22 
GeneralFix needed in header control PinmemberHogne Fossland5-Mar-06 22:56 
Questioneasier way? Pinmembervfilll2-Oct-05 7:14 
AnswerRe: easier way? PinmemberHelen_Bakulina3-May-06 6:20 
AnswerRe: easier way? PinmvpDavidCrow11-May-07 3:49 
GeneralCListCtrlEx Pinmemberphoenixwu27-Jun-05 4:41 
GeneralOT: You might think about replacing that image... PinmemberJohann Gerell10-Jun-05 0:43 
GeneralNice Pinmemberthe-unforgiven8-Jun-05 22:37 
GeneralRe: Nice PinmemberAlexander M.9-Jun-05 6:42 
GeneralRe: Nice PinmemberMMs_xH30-Jun-05 3:40 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 9 Jun 2005
Article Copyright 2005 by Chen Hao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid