Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys

I got a listcontrol element in my MFC-dialog (View->Report).
What I wanted: 2 columns, all rows read out from database

I tried:
LVITEM lvItem;

m_List.InsertColumn(1, "First Column", 0, 150, 1);   // could be created
m_List.InsertItem(1, "First Row");   // could NOT be created

"InsertItem.." could not be created, so I tried another way...
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
lvItem.pszText = "Row one";
m_List.InsertItem(&lvItem);


What am I doing wrong or do I have to add something else?


Greetings
Epanjohura
Posted

Item and column indexes start at 0. If you want to have two columns, you must insert two:
C++
InsertColumn(0, _T("Column 1"), LVCFMT_LEFT, 100, 0);
InsertColumn(1, _T("Column 2"), LVCFMT_LEFT, 100, 1);

Then add items to the list where InsertItem() specifies the main item which is shown in the first column and the additional columns are specified by SetItemText():
C++
InsertItem(0, _T("Item 1-1"));
SetItemText(0, 1, _T("Item 1-2"));
InsertItem(1, _T("Item 2-1"));
SetItemText(1, 1, _T("Item 2-2"));
 
Share this answer
 
Comments
epanjohura 14-Feb-13 3:29am    
I tried your solution, but it's the same problem as I had before... the program crashes by the line "setItemText(...)"
Jochen Arndt 14-Feb-13 3:42am    
Then there must be something else wrong. At first you can put all calls into VERIFY() macros to see which function fails first (requires debug build):
VERIFY(m_List.InsertColumn(...) >= 0);
VERIFY(m_List.InsertItem(...) >= 0);
VERIFY(m_List.SetItemText(...) != 0);
Ensure also that the control uses the report style:
ASSERT((m_List.GetStyle() & LVS_TYPEMASK) == LVS_REPORT);
Marius Bancila 14-Feb-13 4:20am    
You must have the LVS_REPORT style to be able to display multiple columns.
Well, it was such a stupid fault...

I tried and tried...finally, I just had to change some properties:

Owner Data : False
Owner Draw Fixed : False


Greetings
Epanjohura
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900