Parsing CSV: missing first record?

Sean909

New member
Joined
Dec 30, 2008
Messages
4
Programming Experience
Beginner
Hi there,
When parsing csv file into a datatable, it doesn't show the first line in a datagridview. What am I missing here?

thanks!


VB.NET:
Dim dt As New DataTable
        Dim i As Integer
        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\employee.txt")

            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {","}
            Dim f As Integer = MyReader.ReadFields.Length
            For i = 1 To f
                dt.Columns.Add()
            Next
            Do While Not MyReader.EndOfData
                
                Try
                    dt.Rows.Add(MyReader.ReadFields())
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & _
                    " is invalid.  Skipping")
                End Try
            Loop
        End Using
        Me.DataGridView1.DataSource = dt
 
Last edited by a moderator:
You have a ReadFields line that discards the fields read from first line, this one:
VB.NET:
Dim f As Integer = MyReader.ReadFields.Length
 

Latest posts

Back
Top