2

I don't know how to load my CSV file load into a datagrid view in vb2005.

I have two records as the following

9,N,010324405,,,,,,,,05071958,UU,Yoeun,,,,,,,,,,,M,M,KHM,,P,RESID,,,,,"St. Lum,Phum Ti Pir,Chrouy Changva,Ruessei Kaev",,,,PP,,KHM,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,215,S,,DDD_70300098149,21082012,PLN,KHR,6000,1206013,N,N,6000,M,,12042013,67000.00,NO,120000,0,12052013,0,30042013,,,,

10,N,00032529,,,,,,,,18021962,SDM,Sok,,,,,,,,,,,M,M,KHM,,P,RESID,,,,,"#281,Phum Ti Muoy,Chrouy Changva,Ruessei Kaev",,,,PP,,KHM,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,215,S,,DDD_703004167,20022013,PLN,KHR,10000,1510013,N,N,12000,M,,1802013,15660.00,NO,75000,0,1505213,0,3002013,,,,

My code:

For Each line As String In System.IO.File.ReadAllLines(pathname)
        DataGridView1.Rows.Add(line.Split(","))
Next

I want to read this format load to datagridview, any solution?

1
  • 1
    Don't use split, it will generate wrong result. Commented Jul 12, 2013 at 8:14

3 Answers 3

5

Try the following code

Dim TextFieldParser1 As New Microsoft.VisualBasic.FileIO.TextFieldParser(pathname)

TextFieldParser1.Delimiters = New String() {","}

While Not TextFieldParser1.EndOfData
    Dim Row1 As String() = TextFieldParser1.ReadFields()

    If DataGridView1.Columns.Count = 0 AndAlso Row1.Count > 0 Then
        Dim i As Integer

        For i = 0 To Row1.Count - 1
            DataGridView1.Columns.Add("Column" & i + 1, "Column" & i + 1)
        Next
    End If

    DataGridView1.Rows.Add(Row1)
End While
Sign up to request clarification or add additional context in comments.

Comments

0

Check the example with CSV library:

at Codeproject

Comments

0

You can follow this url, using OLEDBCommand you can read CSV format. Take a look at p.campbell's solution.

2 Comments

can give me example coding.
Have you checked the link and tried p.campbell's answer ? Its an example code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.