打开并将多个txt或csv文件导入DataGridView

时间:2017-05-04 10:03:53

标签: vb.net csv datagridview text-files

我的编码有问题。我想将多个文本文件导入DataGridView。但是我做的这个程序是在读取下一个文件时不断更新数据,而不是在另一个表中显示。

已编辑:我想找到如何编辑我的代码,以便另一列上的另一个文本文件数据

Private Function loadmultipletxtfile()

    OpenFileDialog1.InitialDirectory = "c:\temp\"
    OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"
    'OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True
    Me.OpenFileDialog1.Multiselect = True

    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        For x = 0 To OpenFileDialog1.FileNames.Count
            Try
                Dim fName As String = ""
                fName = OpenFileDialog1.FileNames(x)
                Me.TextBox1.Text += fName

                Dim TextLine As String = ""

                Dim SplitLine() As String

                If System.IO.File.Exists(fName) = True Then

                    Dim objReader As New System.IO.StreamReader(fName)
                    table.Columns.Add("Id" & x, Type.GetType("System.Int32"))
                    table.Columns.Add("Angle MX" & x, Type.GetType("System.String"))
                    table.Columns.Add("Angle MY" & x, Type.GetType("System.String"))
                    table.Columns.Add("Angle MXK" & x, Type.GetType("System.String"))
                    table.Columns.Add("Angle MYK" & x, Type.GetType("System.String"))
                    DataGridView1.DataSource = table


                    Do While objReader.Peek() <> -1

                        TextLine = objReader.ReadLine()

                        SplitLine = Split(TextLine, ",")

                        'Me.DataGridView1.Rows.Add(SplitLine)
                        table.Rows.Add(SplitLine)
                        DataGridView1.DataSource = table

                    Loop

                Else

                    MsgBox("File Does Not Exist")

                End If
            Catch ex As Exception

            End Try
        Next
        MsgBox("Total files loaded = " & OpenFileDialog1.FileNames.Count)
    End If


End Function '

0 个答案:

没有答案