VB.Net: 在 Datagridview 中打开 CSV 文件

VB.Net: Open CSV File in Datagridview

我在将数据从 CSV 文件加载到 Datagridview 时遇到问题。 尝试读取文件时出现以下错误: "The input array is longer than the number of columns in this table"

Dim SR As StreamReader = New StreamReader(My.Computer.FileSystem.SpecialDirectories.Programs & "/PURTB/Data/" & TextBox1.Text & ComboBox3.Text & ".csv")
    Dim line As String = SR.ReadLine()
    Dim strArray As String() = line.Split(","c)
    Dim dt As DataTable = New DataTable()
    Dim row As DataRow

    For Each s As String In strArray
        dt.Columns.Add(New DataColumn())
    Next

    Do
        line = SR.ReadLine
        If Not line = String.Empty Then
            row = dt.NewRow()
            row.ItemArray = line.Split(",")
            dt.Rows.Add(row)
        Else
            Exit Do
        End If
    Loop

我使用的文件看起来像这样: CSV File

更新:可能问题出在导出代码上。

Dim StrExport As String = ""
    For Each C As DataGridViewColumn In DataGridView1.Columns
        StrExport &= """" & C.HeaderText & ""","
    Next
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine

    For Each R As DataGridViewRow In DataGridView1.Rows
        For Each C As DataGridViewCell In R.Cells
            If Not C.Value Is Nothing Then
                StrExport &= """" & C.Value.ToString & ""","
            Else
                StrExport &= """" & "" & ""","
            End If
        Next
        StrExport = StrExport.Substring(0, StrExport.Length - 1)
        StrExport &= Environment.NewLine
    Next

    Dim tw As IO.TextWriter = New IO.StreamWriter(My.Computer.FileSystem.SpecialDirectories.Programs & "/PURTB/Data/" & TextBox1.Text & ComboBox3.Text & ".csv")
    tw.Write(StrExport)
    tw.Close()

一旦我需要在我的 DataGridView 中导入一个 CSV 文件,我就按照下面的代码做了(它仍然被许多用户使用并且没有错误):

Dim dt As DataTable = New DataTable 'you will not need this if your dgv has a datatable as datasource
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fName As String = ""
    OpenFileDialog1.InitialDirectory = "D:\TestFile"
    OpenFileDialog1.Filter = "CSV files(*.csv)|*.csv"
    OpenFileDialog1.RestoreDirectory = True
    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        fName = OpenFileDialog1.FileName
    End If

    Dim TextLine As String = ""
    Dim SplitLine() As String

    If System.IO.File.Exists(fName) = True Then
        Dim objReader As New System.IO.StreamReader(fName, Encoding.ASCII)
        Dim index As Integer = 0
        Do While objReader.Peek() <> -1
            If index > 0 Then
                TextLine = objReader.ReadLine()
                SplitLine = Split(TextLine, ",")
                dt.Rows.Add(SplitLine)
            Else
                TextLine = objReader.ReadLine()
            End If
            index = index + 1
        Loop
        DataGridView1.DataSource = dt
    Else
        MsgBox("File Does Not Exist")
    End If
End Sub

希望它有效。 ^^

使用 DataTable 时,您需要定义列...否则,您会收到 OP 指定的错误:“输入数组长于此 table" - 如果添加以下代码,上面的代码就可以工作...

    dt.Columns.Add("Column1", GetType(String))
    dt.Columns.Add("Column2", GetType(String))
    dt.Columns.Add("Column3", GetType(String))
    dt.Columns.Add("Column4", GetType(String))
    dt.Columns.Add("Column5", GetType(String))

    etc.