CSV 到 VB.NET 中的 DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17610308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:14:49  来源:igfitidea点击:

CSV to DataGrid in VB.NET

vb.net

提问by Jam Dara

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

我不知道如何将我的 CSV 文件加载到 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?

我想将此格式加载到datagridview,有什么解决方案吗?

回答by David -

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

回答by geekonweb

Check the example with CSV library:

使用 CSV 库检查示例:

at Codeproject

在 Codeproject

回答by NeverHopeless

You can follow this url, using OLEDBCommandyou can read CSVformat. Take a look at p.campbell's solution.

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