2

我不知道如何将我的 CSV 文件加载到 vb2005 中的数据网格视图中。

我有如下两条记录

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,,,,

我的代码:

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

我想读取这种格式加载到datagridview,有什么解决方案吗?

4

3 回答 3

5

试试下面的代码

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
于 2013-07-12T10:20:43.967 回答
0

你可以关注这个url,使用OLEDBCommand你可以阅读CSV格式。看看p.campbell的解决方案。

于 2013-07-12T09:30:57.230 回答
0

使用 CSV 库检查示例:

在代码项目

于 2013-07-12T08:51:36.817 回答