3 Replies - 1126 Views - Last Post: 30 April 2014 - 01:56 PM Rate Topic: -----

#1 fatbunny   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 09-February 09

Listview subitems on wrong row to item

Posted 29 April 2014 - 08:18 AM

I'm trying to parse a csv file to a listview box. The first row in the csv file will be the header, the first column being the item and subsequent columns being the subitems. The subitems are coming out on the wrong row.

Imports System.IO
Public Class Form1

    Private Sub cmdBrowse_Click(sender As System.Object, e As System.EventArgs) Handles cmdBrowse.Click

        Dim openfiledialog1 As New OpenFileDialog
        Dim filename As String = ""
        Dim splitsline() As String
        Dim splitsword As String()
        Dim listviewcol As New ColumnHeader
        Dim lvwitem As New ListViewItem


        If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtfilelocation.Text = openfiledialog1.FileName
        End If

        filename = txtfilelocation.Text

        Try
            Using sr As StreamReader = New StreamReader(filename)
                splitsline = IO.File.ReadAllLines(filename)
            End Using



        Catch ex As Exception
        Finally
            With ListView1
                .View = View.Details
                .FullRowSelect = True
            End With

            For i As Integer = 0 To splitsline(0).Split(",").Count - 1
                splitsword = splitsline(0).Split(",")
                listviewcol = New ColumnHeader
                listviewcol.Text = splitsword(i).ToString
                listviewcol.TextAlign = HorizontalAlignment.Center
                listviewcol.Width = 125
                ListView1.Columns.Add(listviewcol)

            Next
            For x As Integer = 1 To splitsline.Count - 1
                splitsword = splitsline(x).Split(",")
                lvwitem = New ListViewItem
                For y As Integer = 0 To splitsword.Count - 1
                    If y = 0 Then
                        ListView1.Items.Add(splitsword(y).ToString)
                    Else
                        lvwitem.SubItems.Add(splitsword(y).ToString)
                    End If

                Next
                ListView1.Items.Add(lvwitem)

            Next
        End Try


    End Sub
End Class



Here is the file I am parsing

First Name,Last Name,Phone Number
Jim,Roy,2233
Billy,Bob,4432
Sam,stone,3333



And this is what I am getting

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Listview subitems on wrong row to item

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Listview subitems on wrong row to item

Posted 29 April 2014 - 08:35 AM

Using the TextFieldParser class would make this a whole lot easier.

That being said, instead of all the adds you need to build lvwItem based off that for...next block and then Add lvwItem to the listview.

EDIT:
After reading what I posted, I felt that I wasn't clear.

Everytime you add an item (.Items.Add()), you add a new row. Since your for...next does an Items.Add at index 0 of the loop and then when the entire line is read, you get that broken out list like you are showing.

So basically you add a new item if y = 0 containing only the first column then after you add all the subitems, you add a new item containing only the subitems.

Here's a hint that might help you. You can fill your ListViewItems .Text property (which is the first column of the row) instead of using Add. Your listviewitem is currently called lvwitem.

This post has been edited by CharlieMay: 29 April 2014 - 08:55 AM

Was This Post Helpful? 1
  • +
  • -

#3 fatbunny   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 09-February 09

Re: Listview subitems on wrong row to item

Posted 30 April 2014 - 01:42 PM

Thank you for that. I understand now what I was doing wrong, adding two rows, thinking the listviewitem object I was creating was only for subitems, not realising it was an item altogether. Its working now.

The textfieldparser needs a higher framework installed so I don't want to use that (don't know how, still new to all this but not going to read up on it just yet), some of the machines will be older xp machines that don't have the newer frameworks installed.

Imports System.IO
Public Class Form1

    Private Sub cmdBrowse_Click(sender As System.Object, e As System.EventArgs) Handles cmdBrowse.Click

        Dim openfiledialog1 As New OpenFileDialog
        Dim filename As String = ""
        Dim splitsline() As String
        Dim splitsword As String()
        Dim listviewcol As New ColumnHeader
        Dim lvwitem As New ListViewItem


        If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtfilelocation.Text = openfiledialog1.FileName
        End If

        filename = txtfilelocation.Text

        Try
            Using sr As StreamReader = New StreamReader(filename)
                splitsline = IO.File.ReadAllLines(filename)
            End Using



        Catch ex As Exception
        Finally
            With ListView1
                .View = View.Details
                .FullRowSelect = True
            End With

            For i As Integer = 0 To splitsline(0).Split(",").Count - 1
                splitsword = splitsline(0).Split(",")
                listviewcol = New ColumnHeader
                listviewcol.Text = splitsword(i).ToString
                listviewcol.TextAlign = HorizontalAlignment.Center
                listviewcol.Width = 125
                ListView1.Columns.Add(listviewcol)

            Next
            For x As Integer = 1 To splitsline.Count - 1
                splitsword = splitsline(x).Split(",")
                lvwitem = New ListViewItem
                lvwitem.Text = splitsword(0).ToString
                For y As Integer = 1 To splitsword.Count - 1
                    lvwitem.SubItems.Add(splitsword(y).ToString)
                Next
                ListView1.Items.Add(lvwitem)

            Next
        End Try


    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Listview subitems on wrong row to item

Posted 30 April 2014 - 01:56 PM

View Postfatbunny, on 30 April 2014 - 04:42 PM, said:

The textfieldparser needs a higher framework installed so I don't want to use that (don't know how, still new to all this but not going to read up on it just yet), some of the machines will be older xp machines that don't have the newer frameworks installed.
[code]

Hey, fair enough :), sometimes easier isn't always the solution. I just throw it out there.

Glad you got it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1