CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2005
    Location
    Sweden
    Posts
    33

    [RESOLVED] Inserting Items to a ListView window

    Hi
    I've created a ListView window, but I can't figure out how I'm supposed to add items to it. The problem is that I can't know how many columns the ListView will contain. I'm creating the listview window and the columns like this:
    Code:
        RECT Rcl;
        GetClientRect(hWnd, &Rcl); 
        
        hQuery = CreateWindowEx(WS_EX_CLIENTEDGE,
                                WC_LISTVIEW,
                                "",
                                WS_VISIBLE      |
                                WS_CHILD        |
                                WS_BORDER       |
                                LVS_REPORT      |
                                //LVS_EDITLABELS  |
                                WS_EX_CLIENTEDGE,
                                0,
                                0,
                                Rcl.right - Rcl.left,
                                Rcl.bottom - 21 - Rcl.top,
                                hWnd, 
                                (HMENU)ID_RESULT, 
                                GetModuleHandle(NULL),
                                NULL);
        
        TCHAR pText[128];
        
        lvC.mask    = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
        lvC.fmt     = LVCFMT_LEFT;
        lvC.cx      = 100;
        lvC.pszText = pText;
        
        QueryColumns = MyBot->get_fields();
        
        int i;
        std::list<std::string>::iterator it;
        for(i = 0, it = FieldResult.begin(); i < QueryColumns; i++, it++)
        {
            lvC.iSubItem = i;
            strcpy(pText, it->c_str());
            ListView_InsertColumn(hQuery, i, &lvC);
        }
    The program interacts with a MySQL database and retrieves the data that I wan't display in my ListView window from there. This also explains why I can't have static columns, different SELECT querys gives different columns.

    I've been looking at msdn for a solution but I can't find anything usefull. I thought that ListView_InsertItem could help me, but couldn't get it to work. If you know any way to solve this problem, please tell me. I would greatly appreciate it.

    Thanks in advance

    / boolman

  2. #2
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: Inserting Items to a ListView window

    I don't understand your question/problem. You have a problem with the items, then where's you code where you add them? What is the problem, actually?

    If you see only the items in the first column, then you must know that ListView_InsertItem macro can be used only to insert items and set the first item (in the first column). Then, to insert subitems you can use ListView_SetItem() or ListView_SetItemText() (the latter if you have to set the text only)
    Last edited by kkez; February 3rd, 2006 at 11:36 AM.

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Inserting Items to a ListView window

    Not very clear what "couldn't get it to work". From your piece of code ListView_InsertColumn have to work except it's an error in other place. That will insert the columns in your listview control (note: you have also to delete all columns before).
    Furher you must step through the resultset rows.
    For each row call ListView_InsertItem for the first column followed by ListView_SetItem for each subsequent column.
    Below is just an example of using ListView_InsertItem and ListView_SetItem.
    Code:
        LVITEM lvi;
        lvi.iItem = 0;      // row 0
        lvi.iSubItem = 0;   // column 0
        lvi.mask = LVIF_TEXT;
        lvi.pszText = .....;
        ListView_InsertItem(hQuery, &lvi);
    
        lvi.iSubItem = 1; // column 1
        lvi.pszText = .....;
        ListView_SetItem(hQuery, &lvi);
    
        lvi.iSubItem = 2; // column 2
        lvi.pszText = .....;
        ListView_SetItem(hQuery, &lvi);
    ... like kkez already suggested.
    Last edited by ovidiucucu; February 3rd, 2006 at 11:41 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Jun 2005
    Location
    Sweden
    Posts
    33

    Re: Inserting Items to a ListView window

    Hmm, this is a bit awkward. This piece of code should have been posted from the beginning:
    Code:
    // The code from my first post + the code underneath
    
        for(i = 0; i < RowResult.size(); i++)
        {
            for(int j = 0; j < QueryColumns; j++)
            {
                char* Text = new char[42];
                strcpy(Text, "I'm a small penguin");
                
                lvI.mask     = LVIF_TEXT;
                
                lvI.iItem    = i;
                lvI.iImage   = 0;
                lvI.iSubItem = j;
                lvI.pszText  = Text;
                
                if(!SendMessage(hQuery, (UINT)LVM_SETITEM, (WPARAM)0, (LPARAM)&lvI))
                    MessageBox(hWnd, "SendMessage LVM_SETITEM failed", "D'oh!", MB_OK);
                
                delete [] Text;
            }
        }
    I think that I can understand what I've done wrong now. Thank you both very much for answering me. I will try to avoid this kind of mistakes in the future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured