Software & Finance

Visual C++ MFC - CListCtrl - Insert Column and Item

MFC list control - CListrCtrl is widely used in GUI Application as it supports report list view. This enables the users to have row and columns which would simulate like grid functionality. The CListCtrl::InsertColumn(...) function is used to insert a new column in the report view modal. CListCtrl::SetColumnWidth is used for setting the width of the column.


Once columns are inserted, the adding the items to the MFC list control are tricky. As we need to use CListCtrl::InsertItem when we add a new row element for the first column. The subsequent columns for the same row can be filled with CListCtrl::SetItem(...) function. LV_ITEM struct defines the property of the MFC List Ctrl elements - LVIF_TEXT, etc. You can also add images, if required by changing the property.


Source Code


class CSiteMapDlg : public CDialog



      enum { IDD = IDD_TEST_DIALOG };


      CListCtrl m_listCtrl;




void CMyTestDlg::DoDataExchange(CDataExchange* pDX)



    DDX_Control(pDX, IDC_LIST1, m_listCtrl);



static void AddData(CListCtrl &ctrl, int row, int col, const char *str)


    LVITEM lv;

    lv.iItem = row;

    lv.iSubItem = col;

    lv.pszText = (LPSTR) str;

    lv.mask = LVIF_TEXT;

    if(col == 0)






BOOL CMyTestDlg::OnInitDialog()




    m_listCtrl.InsertColumn(0, "Page");

    m_listCtrl.SetColumnWidth(0, 60);


    m_listCtrl.InsertColumn(1, "Last Modified");

    m_listCtrl.SetColumnWidth(1, 80);


    m_listCtrl.InsertColumn(2, "Prioirty");

    m_listCtrl.SetColumnWidth(2, 50);


    m_listCtrl.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0,   



    AddData(m_listCtrl,0,0, "First");

    AddData(m_listCtrl,0,1, "Jan 2011");

    AddData(m_listCtrl,0,2, "Medium");


    AddData(m_listCtrl,1,0, "Second");

    AddData(m_listCtrl,1,1, "Feb 2011");

    AddData(m_listCtrl,1,2, "High");



Click here to download the Visual C++ Project and Executable