Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 203,664 times

Contents

Related Categories

ListView Control - Using Columns

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)
    Next
    ' Loop through surname array
    For i = 0 To UBound(SurnameArray)
        .ListItems(i).ListSubItems.Add i, , SurnameArray(i)
    Next
    ' Loop through address array
    For i = 0 To UBound(AddressArray)
        .ListItems(i).ListSubItems.Add i, ,AddressArray(i)
    Next
End With

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [74] ListView Control

    Posted by SeanH on 14 Nov 2007

    How to sort a ListView control? Here is the code I have written to handle just that. This code also includes a context menu of items I found I needed on almost every ListView I used (I use them alo...

  • Re: [74] ListView Control

    Posted by SeanH on 14 Nov 2007

    [quote user="Maxjonz"]

    I found that the following code didn't work for me


    ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item...

  • Re: [74] ListView Control

    Posted by anatha1 on 10 Aug 2007

    Hey everyone here are some example of Database that use ListView:


    if u need in C# pls copy this code convert to C# your self.



    1. Use Microsoft Access Database

    ...

  • Re: [74] ListView Control

    Posted by Halina on 08 Aug 2007

    Hi..


    I using ListView in C#.


    I tried to add text into certain colum in ListView.


    For example: I have 3 columns, Column1, Column2, Column3.


    I want to insert "TEST1"&n...

  • Re: [74] ListView Control

    Posted by Maxjonz on 05 Jul 2007

    I found that the following code didn't work for me


    ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item


    I had t...