Adding Data to a ListView Control

waraq

Member
Joined
Dec 18, 2006
Messages
23
Location
Harrison, TN
Programming Experience
Beginner
List View Display

I have a problem putting all the text that I read from different text files and then place on the listview. The idea is to read each single file and place it on the corresponding column on the listview that should be like this:

Full Name | Address | Email |
John Smith 123 Washington comcast.net and so on...
Abrahan L 124 WallStreet Google.com

Instead I got this:

Full Name | Address | Email
John Smith Abraham L 123 Washington 124 WallStreet comcast.net and so on

This is what I did:
VB.NET:
Dim NS As StreamReader = File.OpenText(Nombres)
        Nomb = NS.ReadToEnd
        NS.Close()
        Dim LCosa As New ListViewItem
        If Nomb <> "" Then
            LCosa.Text = Nomb
        Else
            LCosa.SubItems.Add("Not Entry")
        End If
        Dim Dia As StreamReader = File.OpenText(Direccion)
        Dire = Dia.ReadToEnd
        Dia.Close()
        If Dire <> "" Then
            LCosa.SubItems.Add(Dire)
        Else
            LCosa.SubItems.Add("Not Entry")
        End If
        Dim CRO As StreamReader = File.OpenText(Email)
        CorreoEle = CRO.ReadToEnd
        CRO.Close()
        If CorreoEle <> "" Then
            LCosa.SubItems.Add(CorreoEle)
        Else
            LCosa.SubItems.Add("No Entry")
        End If
lvData.Items.Add(LCosa)
I wil apreciate your help

Willy
 
Last edited by a moderator:
Well, you're only creating one ListViewItem for a start. Obviously you're going to have to create more than one ListViewItem if you want more than one row of data.

Secondly, you're calling ReadToEnd on each StreamReader. Nowhere are you separating the file contents into the fields that belong to each record.

Instead of jumping in and writing code when you really have no idea what the code is suppsoed to be doing, stop and think about the problem. Ask yourself what steps you would have to perform if you were going to be doing this manually. Write down those steps with a real pen and paper. Only when you can take any input data and work your way through your steps and get the correct result should you even consider writing any code. Only then do you actually know what that code is supposed to be implementing.
 
Listviews are a little tricky, especially at first.
They will display your subitems depending on how it was setup in deign mode.
Try first by clicking on the smart-tag on the Listviews control and manually add your item and subitems to make sure it will display properly.
Their are some optimizations you can do with your code, but it all seems correct.

Good Luck.
 
Back
Top