Vb.net 读取csv文件不需要';我什么也没展示?

Vb.net 读取csv文件不需要';我什么也没展示?,vb.net,csv,datagridview,Vb.net,Csv,Datagridview,我是visualbasic的初学者,正在尝试将csv文件导入datagridview 我使用的示例代码如下所示: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Using MyReader As New Microsoft.VisualBasic. FileIO.TextFieldParse

我是
visualbasic
的初学者,正在尝试将
csv
文件导入
datagridview

我使用的示例代码如下所示:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  Using MyReader As New Microsoft.VisualBasic.
                FileIO.TextFieldParser("C:\test\text.txt")
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Application.DoEvents()
        Try
            currentRow = MyReader.ReadFields()
            With DataGridView1
                .ColumnCount = 2
                Dim row As String() = New String() {currentRow(0), currentRow(1)}
                .Rows.Add()
            End With
        Catch ex As Microsoft.VisualBasic.
                    FileIO.MalformedLineException
            MsgBox("Line " & ex.Message &
            "is not valid and will be skipped.")
        End Try
    End While
  End Using
End Sub
Date,distance
1,5
2,8
4,9
10,15
我使用的数据如下:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  Using MyReader As New Microsoft.VisualBasic.
                FileIO.TextFieldParser("C:\test\text.txt")
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Application.DoEvents()
        Try
            currentRow = MyReader.ReadFields()
            With DataGridView1
                .ColumnCount = 2
                Dim row As String() = New String() {currentRow(0), currentRow(1)}
                .Rows.Add()
            End With
        Catch ex As Microsoft.VisualBasic.
                    FileIO.MalformedLineException
            MsgBox("Line " & ex.Message &
            "is not valid and will be skipped.")
        End Try
    End While
  End Using
End Sub
Date,distance
1,5
2,8
4,9
10,15
但是,在使用上述代码导入
csv
文件后,我的
datagridview
变为空白

输出如下所示:


带有以下代码

With DataGridView1
    ...
    .Rows.Add()
End With
通过调用
Add
创建一个空行,无需任何参数

你可能想打电话

.Rows.Add(row)

可能很愚蠢,但在代码的文件路径部分,您编写了text.txt,而不是.csvYeah,我忘了更改文件的扩展名。太棒了。工作完美。谢谢。如何将datagridview中的数据保存到新文件?很抱歉问了这个封闭的问题。我尝试过这样做:
SaveFileDialog1.ShowDialog()System.IO.File.WriteAllText(SaveFileDialog1.FileName,DataGridView1.Rows)
,但不知道这是否正确。我建议使用类似的CSV库。它允许您使用类来表示数据并将其读入数组。然后,您可以将其绑定到DataGridView并轻松地将其保存回磁盘。谢谢。我会调查档案管理员