CListCtrl problem

I've created a MFC doc-view project which uses the CListView class. I'm using the CListView (and the underlying CListCtrl in effect) in Report View mode. I have added 3 columns to the CListCtrl using the following code :

CListCtrl &pList = GetListCtrl();
int width;

width = (rect.bottom - /3;
if ((pList.InsertColumn(0,"SourceName", LVCFMT_LEFT,width)) == -1)
   AfxMessageBox("Could not insert column 0");
if ((pList.InsertColumn(1,"Computer Name",LVCFMT_LEFT,width,0)) == -1)
   AfxMessageBox("Could not insert column 1");
if ((pList.InsertColumn(2,"Event Type",LVCFMT_LEFT,width,1)) == -1)
   AfxMessageBox("Could not insert column 2");

After doing this I'm trying to insert items using InsertItem as follows :

for(countofitems = 0; countofitems < 3; countofitems++) {
item.mask = LVIF_TEXT;
item.iItem = countofitems;
if (countofitems == 0) {
   item.pszText = SourceName;
   item.iSubItem = 0;
} else if (countofitems == 1) {
   item.pszText = ComputerName;
   item.iSubItem = countofitems + 1;
} else if (countofitems == 2) {
   item.pszText = buf.GetBufferbuf.GetLength());
item.iSubItem = countofitems + 1;

However, only the first column's data gets inserted (and is seen) not the next two (ie ComputerName and the variable buf).

Any pointers ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should use CListCtrl::InsertItem to insert the main items and use CListCtrl::SetItemText to set the text of the subitems. CListCtrl::InsertItem cannot insert subitems.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
???? width=(rect.bottom -;

I think it should be:
width = (rect.right - rect.left)/3;
Ensure the following order for item structure

fofirst row first column

item.iitem = 0; item.isubitem = 0;

for  first row second column
item.iitem = 0; item.isubitem = 1;

for  first row second column
item.iitem = 0; item.isubitem = 2;

fstabAuthor Commented:
Thanks to all who responded. However, may I mention that I was also getting some other problems.

I noticed that though I used SetItemText, not all the subitems were getting set to the text I specified, and were random in nature.

I later discovered that this malady (correct me if I'm wrong anyone) was due to my setting the styles to : |= LVS_REPORT | LVS_SINGLESEL | LVS_SORTASCENDING;

Don't see it ? Its due to the LVS_SORTASCENDING being set .. On removing this, all subitems were seen.

Thanks all again - esp. VBapat for telling me that teeny-weeny bit about the right - left !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.