ListView Scenario 2: Using a Procedure to Create ColumnHeaders

See Also

The code examples in this topic are taken from the DataTree.vbp sample application, which is listed in the Samples directory.

In the previous scenario, "ListView Control Scenario 1: Using the ListView Control with the TreeView Control," the ListView control and the TreeView control are showed working in tandem. In that scenario, the TreeView control's NodeClick event is used to call two procedures. The first procedure, "MakeColumns," which creates ColumnHeader objects, is outlined here.

The following example uses this object:

To create ColumnHeader objects are

  1. Clear the ColumnHeaders collection with the Clear method and create ColumnHeaders.

  2. Use the Add method to create ColumnHeader objects.

Clear the ColumnHeaders collection with the Clear method and Create ColumnHeaders

The procedure first clears all members of the ColumnHeaders collection using the Clear method:

lvwDB.ListItems.Clear

This step is necessary if you intend to create different sets of ColumnHeader objects for different tables. For example, you may want to populate the ListView control not only when the Publisher nodes are clicked, but also when the user clicks the root of the tree. In that case, the ListView control would be populated with a different list.

Use the Add Method to Create ColumnHeader Objects

After clearing the ColumnHeaders collection, you use the Add method to add ColumnHeader objects to the collection, as shown:

Private Sub MakeColumns()
   ' Clear the ColumnHeaders collection.
   lvwDB.ColumnHeaders.Clear
   ' Add four ColumnHeaders.
   lvwDB.ColumnHeaders.Add , , "Title", 2000
   lvwDB.ColumnHeaders.Add , , "Author"
   lvwDB.ColumnHeaders.Add , , "Year", 350
   lvwDB.ColumnHeaders.Add , , "ISBN"
End Sub

Note that the Add method syntax allows you to set the Width property for each ColumnHeader object. In the above code, the Width property is set only for the "Title" and "Year" ColumnHeader objects.