views:

45

answers:

2

I'm having trouble adding items onto a ListView control. When I try to add items to my ListView, nothing happens. I was using this code before, and it didn't work.

I have 3 columns, with the SubItem values set to 1, 2, and 3.

   LVITEM item;
   item.mask = LVIF_TEXT;
   item.cchTextMax = 6;

   item.iSubItem = 1;
   item.pszText = TEXT("12345");
   item.iItem = 0;
   ListView_InsertItem(hListView, &item);

   item.iSubItem = 2; // zero based index of column
   item.pszText = TEXT("23456");
   ListView_InsertItem(hListView, &item);

   item.iSubItem = 3; // zero based index of column
   item.pszText = TEXT("34567");
   ListView_InsertItem(hListView, &item);
A: 

Have you called ListView_InsertColumn (link)? A column will not show up in a ListView in "details" mode (the columned one) until you do. Thus, if you've not added any columns, you won't see anything. (Regardless of if you're using headers or not)

Thanatos
I had already added the column, but for some reason, nothing was showing up. Anyway, the code somehow fixed itself.
myeviltacos
+2  A: 

From MSDN:

You cannot use ListView_InsertItem or LVM_INSERTITEM to insert subitems. The iSubItem member of the LVITEM structure must be zero. See LVM_SETITEM for information on setting subitems.

Try using ListView_SetItem() for the secondary columns (subitems 1 and 2) after adding the first column (subitem 0) with ListView_InsertItem().

gwell
Thanks, that worked. :)
myeviltacos