February 16, 2019
Hot Topics:

Adding Database Attribute Rows to the List Control

  • November 12, 2004
  • By Nancy Nicolaisen
  • Send Email »
  • More Articles »

In this lesson, you'll see how to add database attribute rows to the list view control that has been constructed and initialized in previous examples. You will explore these techniques by examining the functioning of the RemoteDBScan example. Sources are available on this example Web site in the Wireless/Moble section.

The Anatomy of the Listview Control

The Listview Control treats its contents as a matrix, where iItem is the zero-based row index and iSubItem is the zero-based column index. Although the CListViewEx class on which your view is based appears to the user to handle the list control contents as a collection of horizontal rows, the underlying mechanism actually organizes and manages content as a collection of row/column matrix elements. For this reason, when the "row" containing a database's attributes is added, each item must be added separately.

Adding a list item is not unlike adding a column to the list control, in that it is a two-step process. First, initialize an LV_ITEM structure that defines the item's attributes; then add the item with a call to the ClistCtrl member InsertItem().

Here's the typedef for the LV_ITEM structure:

typedef struct _LVITEM {
        UINT   mask;        //flags that define which members are
        int    iItem;       //0-based row index in list ctrl matrix
        int    iSubItem;    //0-based column index in list control
        UINT   state;       //the item's state, state image, and
                            //overlay image
        UINT   stateMask;   //tells which bits of the state are
        LPTSTR  pszText;    //item caption
        int    cchTextMax;  //caption buffer length
        int    iImage;      //index of image in image list
        LPARAM lParam;      //used for app-specific data

Tables 1 and 2 show the possible values for the mask and state members of the LV_ITEM structure:

Table 1: LV_ITEM Mask Flags and their Meanings

Mask Flag Constant Meaning
LVIF_TEXT pszText is valid
LVIF_IMAGE iImage is valid
LVIF_INDENT iIndent is valid
LVIF_PARAM lParam is valid
LVIF_STATE state is valid

Table 2: LV_ITEM State Flags and their Meanings

ItemState Flag Constant Meaning
LVIS_ACTIVATING Item is activating on LVN_ITEMACTIVATE notification
LVIS_CUT Item is marked for cut and paste
LVIS_DROPHILITED Item is a drag and drop target
LVIS_FOCUSED Item has the input focus
LVIS_SELECTED Item is selected

Now, take a tour of the AddRowToList() member function.

BOOL CRemoteDBScanView::AddRowToList(CEOID enumCeoid,
                                     CEOIDINFO * poidInfo )

    LV_ITEM         lvitem;    //List View Item Descriptor struct

    //Get the List Control Object
    CListCtrl& ListCtrl = GetListCtrl();
    // add at the last row
    lvitem.iItem     = ListCtrl.GetItemCount();
    lvitem.mask      = LVIF_TEXT | LVIF_STATE;
    lvitem.stateMask = LVIS_FOCUSED;
    lvitem.state     = LVIS_FOCUSED;

This function is designed to add a full row at the bottom of the list control, so the first step is to get a count of list control items. Recall that "items" are synonymous with rows in this context, whereas subitems are synonymous with columns.

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date