Adding a List of Items to a ListView Control

When you first create a ListView control, it is empty and has no ListItem or ColumnHeader objects. To add items to the control, you can add a ListItem to the ListItems collection using the Add method. The Add method enables you to specify the Index, Key, Text, Icon, and SmallIcon properties when you add the new ListItem. In addition, the Add method returns a reference to the new ListItem, so you can set the other properties directly.

A ListView control has three views that display items using the Text property. You can set the appearance of the control using the View property. To display the list with no icons, set View to lvwList. To display the list with icons, set View to lvwIcon for large icons or lvwSmallIcon for small icons. You can set the icons by setting the Icons and SmallIcons properties to the hImageList property of an ImageList control.

You also can set a ListView control to Report view (lvwReport). In report view, you can view column headers and the items defined by the SubItems property. To add column headers, use the Add method of the ColumnHeaders collection to specify the text of the ColumnHeader. Because the Add method returns a reference to a ColumnHeader, you can continue to change its properties, such as Width or SubItemIndex.

After you add data to the two collections of the ListView control, you can respond to user events.