ListView Control

Using Columns

The ListView control allows you to add columns of data to the list view. Normally, you know the columns you want, and can add them at design time. To do this, right click on the ListView control and click properties. This displays the property sheet. Now click on the Column Headers tab. Click on the Insert Column button, and fill out the data. The minimum data you need to add is the text to be displayed. It is also helpful if you add a value into the Key field, as you can then easily identify the column using that key. If you want, you can also add a Image index so an image will be displayed next to the column text (see using icons, below for more info)

If you want to add the columns at run time, you can use the ColumnHeaders collection and add them like adding list items (see above)

In order for the columns to be displayed, you need to set the View mode to Report. To do this at design time, view the properties and change View to Report. Note that the columns you have added will not be displayed at design time. Alternatively you can set the view at runtime:

ListView1.View = lvwReport

Add a few columns at design time, and add the code above, and the code you wrote in the Adding Items section to the form_load event. Run the project. You will notice that the items you have added, only has the text in the first column. To add values to other columns, you need to use the ListSubItems collection, which is a sub property of the ListItems collection (see Adding Items). For example:

ListView1.ListItems(1).ListSubItems.Add , "key1", "text1"

adds a sub item to the first item on the list with the key, Key1, and the text, Text1. You can also use the SubItems property as an easier method, if you only want to set the text:

ListView1.ListItems(1).SubItems(1) = "Text1"

This adds Text1 to the second column of the first item.

The following code adds the items shown in Adding Items, but also adds some extra data in the other columns:

' Set the View
ListView1.View = lvwReport
' Add the columns
With ListView1.ColumnHeaders
    .Add , , "Name"
    .Add , , "Surname"
    .Add , , "Address"
End With

With ListView1.ListItems
    ' Add the normal text
    .Add , , "Fred"
    .Add , , "Sarah"
    .Add , , "Paul"
End With
With ListView1
    ' Add a value to the second column to the first item on the list ("Fred")
    ' (1) = First item on list
    .ListItems(1).SubItems(1) = "Crowley"
    .ListItems(2).SubItems(1) = "Ives"
    .ListItems(3).SubItems(1) = "Smith"
    ' Add a value to the third column to the first item on the list ("Fred")
    ' (1) = First item on list
    .ListItems(1).SubItems(2) = "16 Liverpool Lane"
    .ListItems(2).SubItems(2) = "102 England Street"
    .ListItems(3).SubItems(2) = "1 Baker Street"
End With

Form1 should then look like this:

listview.gif (3896 bytes)

Of course, this is when you know what items you want to add. When you don't, you can loop through an array that has all the items in:

With ListView1
    ' Loop through name array
    For i = 0 To UBound(NameArray)
        .ListItems.Add i, ,NameArray(i)
    ' Loop through surname array
    For i = 0 To UBound(SurnameArray)
        .ListItems(i).ListSubItems.Add i, , SurnameArray(i)
    ' Loop through address array
    For i = 0 To UBound(AddressArray)
        .ListItems(i).ListSubItems.Add i, ,AddressArray(i)
End With

You might also like...


About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous