Click here to Skip to main content
13,596,895 members
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 9 Jul 2000

Simple Row Insert for CListCtrl

, 9 Jul 2000
Rate this:
Please Sign up or sign in to vote.
This simple CListCtrl extension allows easy row insertion when in Report mode.
<!-- STEP 2. Include download and sample image information -->

Ever been annoyed at the amount of code you need just to insert a few rows and their columns' text into a CListCtrl while using the 'Report' style?

This little MFC subclassing tutorial provides a handy little enhancement to the standard CListCtrl, which can make life easier for both beginners and not_so_beginners alike.

The code and text that follows assumes you have generated a typcal Dialog based application with the MFC Application (.exe) Appwizard in MS Visual C++.

Derive a new CListCtrl based class (here CMyListCtrl).  In the VC IDE, this is as simple as selcting 'Add Class...' from the MFC Class wizard then, in the new class dialog, naming the control and selecting CListCtrl as the base class in the drop down list.

To use the new class, start by adding a CListCtrl to the dialog (be sure to select 'Report' in the Styles page of the proprties for the new control) then, in the Member Variables page of the MFC Class Wizard (ctrl+w), add a control variable for the newly created item (IDC_LIST1, or whatever ID you supplied) - but instead of accepting the default CListCtrl as the type, you will be able to select the new CMyListCtrl type. Call the variable m_myList.


<font color="#0000FF">#include</font> "MyListCtrl.h"

in the header of the dialog class (in this case, the main dialog of the app) and you're set.

In the code that VC generates for the new class, add the following: 

<font color="#008040">// put this into the header file, in the public: section
color="#0000FF">int</font> InsertRow(<font color="#0000FF">int</font> nPos,<font
color="#0000FF">int</font> nNoOfCols, LPCTSTR pText, ...);
<font color="#008040">// and this into the cpp file, in the public: section</font>
color="#0000FF">int</font> CMyListCtrl::InsertRow(<font color="#0000FF">int</font> nPos,<font
color="#0000FF">int</font> nNoOfCols, LPCTSTR pText, ...)
    va_list argList;
    va_start(argList, pText);

    ASSERT(nNoOfCols >= 1); <font
color="#008040">// use the 'normal' Insert function with 1 argument</font>
color="#0000FF">int</font> nIndex = InsertItem(LVIF_TEXT|LVIF_STATE, nPos, pText,0,LVIS_SELECTED,0,0);
    ASSERT(nIndex != -1);
color="#0000FF">if</font> (nIndex < 0) <font color="#0000FF">return</font>(nIndex);
color="#0000FF">for</font> (<font color="#0000FF">int</font> i = 1; i < nNoOfCols; i++) {
        LPCSTR p = va_arg(argList,LPCSTR);
color="#0000FF">if</font> (p != NULL) {
            SetItemText(nIndex, i, p);    
<font color="#008040">// In your program the list control can now be filled like this:
   myList.InsertRow(0,3,_T("Sample 1"),_T("Column 2 for row 1"),_T("Column 3 for row 1"));
   myList.InsertRow(1,3,_T("Sample 2"),_T("Column 2 for row 2"),_T("Column 3 for row 2"));
   myList.InsertRow(2,3,_T("Sample 3"),_T("Column 2 for row 3"),_T("Column 3 for row 3"));
   myList.InsertRow(3,1,_T("Sample 4")); // nothing for column 2 and 3
   myList.InsertRow(4,3,_T("Sample 5"),_T("Column 2 for row 5"),_T("Column 3 for row 5"));
   myList.InsertRow(5,2,_T("Sample 6"),_T("Column 2 for row 6")); // nothing for column 3

To test, you might add a command button that executes the above code.  Also, this assumes that you have initialized the list with 3 columns - for the sake of this simple example, you might provide the following in the OnInitDialog method of the dialog class:

<font color="#008040">// TODO: Add extra initialization here</font>
m_myList.InsertColumn(0, "Column 1");
m_myList.InsertColumn(1, "Column 2");
m_myList.InsertColumn(2, "Column 3");

m_myList.SetColumnWidth(0, 100);
m_myList.SetColumnWidth(1, 100);
m_myList.SetColumnWidth(2, 100);




This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Daniel Zuppinger
Chief Technology Officer
Switzerland Switzerland
Professional IT developer since 1983. First projects with Cobol, then Pascal, Modula2, C and since Visual C++ 1.0 also with C++. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, from 2008 until 2013 for enValue (also Switzerland) and currently I'm working for Comfone (Bern, Switzerland).

Married, two daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

You may also be interested in...

Comments and Discussions

GeneralPreventing auto resize in a CListCtrl Pin
paratracker4-Jun-08 3:41
memberparatracker4-Jun-08 3:41 
GeneralCListCtrl Item Number reorder. Pin
chuck5511-Apr-08 8:30
memberchuck5511-Apr-08 8:30 
QuestionHow to add items in list control(Multicolumn)? Pin
Arum M S26-Jul-06 20:42
memberArum M S26-Jul-06 20:42 
QuestionCListCtrl Pin
Arum M S26-Jul-06 20:25
memberArum M S26-Jul-06 20:25 
GeneralSimple problem, please help Pin
haiaw12-Sep-04 23:42
memberhaiaw12-Sep-04 23:42 
Generalcan't retrieve subitem text Pin
polonskyg23-May-04 11:58
memberpolonskyg23-May-04 11:58 
GeneralDifferent width for different row and column place Pin
L. Naga Prasad19-May-04 4:05
memberL. Naga Prasad19-May-04 4:05 
Questionhow to show multilanguage using SetItemText? Pin
cheonyong2-May-04 19:11
membercheonyong2-May-04 19:11 
AnswerRe: how to show multilanguage using SetItemText? Pin
manuchadha6-Jan-05 19:31
membermanuchadha6-Jan-05 19:31 
QuestionHow to limit the number of columns in a List Control Pin
pandey_sachin26-Jan-04 2:13
memberpandey_sachin26-Jan-04 2:13 
GeneralList Control in sub-Dialog Pin
bnn3nasdfasdfa2-Jan-04 8:21
memberbnn3nasdfasdfa2-Jan-04 8:21 
GeneralDelete a row from an SDI List View And Serialization of tthe Code Pin
fesi4-Apr-03 3:18
memberfesi4-Apr-03 3:18 
QuestionHow to break the 260 character limit? Pin
Wicket_13-Feb-03 22:07
sussWicket_13-Feb-03 22:07 
AnswerRe: How to break the 260 character limit? Pin
Andrew Bond18-Dec-03 7:45
memberAndrew Bond18-Dec-03 7:45 
GeneralList Item reordering Pin
mfregeau9-Nov-02 8:01
sussmfregeau9-Nov-02 8:01 
GeneralList Control in C Pin
GOLUM30-May-02 4:20
memberGOLUM30-May-02 4:20 
GeneralRe: List Control in C Pin
casti23-Sep-05 23:54
membercasti23-Sep-05 23:54 
GeneralList Control in C Pin
GOLUM30-May-02 4:20
memberGOLUM30-May-02 4:20 
GeneralList Control size Pin
Zoltán Alexin27-Nov-01 23:06
memberZoltán Alexin27-Nov-01 23:06 
GeneralCombination between ListBox and EditBox Pin
Becks718-Nov-01 3:40
memberBecks718-Nov-01 3:40 
GeneralListCtrl Pin
rel11-Jul-01 23:48
memberrel11-Jul-01 23:48 
GeneralRe: ListCtrl Pin
David Fleming7-Mar-02 4:22
memberDavid Fleming7-Mar-02 4:22 
GeneralWhy use this class for nothing Pin
BLaZe14-Jun-01 10:52
memberBLaZe14-Jun-01 10:52 
GeneralRe: Why use this class for nothing Pin
Daniel Zuppinger15-Jun-01 3:38
memberDaniel Zuppinger15-Jun-01 3:38 
GeneralRe: Why use this class for nothing Pin
Ruge7-Nov-01 11:06
memberRuge7-Nov-01 11:06 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 10 Jul 2000
Article Copyright 2000 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid