Avatar of disrupt
disrupt
Flag 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
                    Try
                        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")
                            'item.SubItems.Add(NameHeader)
                            item.SubItems.Add(currentRow(5))
                        Else
                        End If

                End While
            End Using

Open in new window

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Dabas

8/22/2022 - Mon
Bob Learned

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?
pgnatyuk

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.


PockyMaster

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

Connect to your CSV file using oleDB
http://www.connectionstrings.com/textfile
Bind the resulting table to your grid, and you're done.
Your help has saved me hundreds of hours of internet surfing.
fblack61
disrupt

ASKER
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
ASKER CERTIFIED SOLUTION
Dabas

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.