Link to home
Start Free TrialLog in
Avatar of disrupt
disruptFlag for United States of America

asked on

Visual Basic 2008 CSV Headers

I need to enumerate the CSV headers and search for a header name and save it's column number to the currentRow like this I can add it to a listView. I have a code example and commented code of what im trying to do.  How can I do this?
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filepath)

                MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                MyReader.Delimiters = New String() {","}
                MyReader.CommentTokens = New String() {""}
                MyReader.HasFieldsEnclosedInQuotes = True

                Dim currentRow As String() = MyReader.ReadFields

                While Not MyReader.EndOfData
                        currentRow = MyReader.ReadFields()
                        Dim item As ListViewItem
						' Dim NameHeader As Coloumn Header
						' for each header in headers if header = "name"  NameHeader = currentRow(i)

                        If currentRow.Length = 10 Then
                            item = ListView1.Items.Add("TEST")
                        End If

                End While
            End Using

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't know how I can help you...if the header is the first row, then you should be able to read all the column headers in one shot...what problems are you having?
What do you mean CSV headers?
CSV is Coma Separated Values?
so it is a text file like:
1, Jon Smith, student, Mathematics
2, Bred Pit, student, physics

each line is a record in a table. there is the coma between the columns. There is the line feed and carriage return in the end of each line.

Any reason why you are using a listview instead of a datagrid?

Connect to your CSV file using oleDB
Bind the resulting table to your grid, and you're done.
Avatar of disrupt


Sorry if I wasn't clear lets say the CSV reads as

Name Subject
John Smith Math
Bred Pit physics

The way I have the code above pickes up the the students and starts at line 0 by reading John Smith ..Math etc as currentRow(0) how can using the code above pick up Name. Subject etc im trying to match Name and Subject etc with other strings to see if the document contains those specified strings I think somewhere in my code I have it that it skips to John Smith... etc
Avatar of Dabas
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial