Open multiples csv files in the folder using vb.net

Viewed 17

The following codes below should work to load all csv files data in the datagridview by why I am getting the error of the method of operation is not implemented. Here are the codes. Do u guys have any solution

Imports System.IO

Imports System.Text Public Class ReadBo

Public Shared Function ReadFile(ByVal FilePath As String) As DataTable

    Try
        Dim pathway As String() = Directory.GetFiles(FilePath)
        Dim dt As New DataTable
        dt.Columns.Add("File name")


        For item As Integer = 0 To pathway.Length - 1
            Dim files As New FileInfo(pathway(item))
            Dim dr As DataRow = dt.NewRow()

            Dim Lines As String() = File.ReadAllLines(files.ToString())
            Dim Fields As String()
            Fields = Lines(0).Split(New Char() {","})
            Dim Cols As Integer = Fields.GetLength(0)


            If item = 0 Then
                For i As Integer = 1 To Cols
                    dt.Columns.Add("Col" & i, GetType(String))
                Next
            End If

            For index As Integer = 0 To Lines.GetLength(0) - 1
                Fields = Lines(index).Split(New Char() {","})
                dr = dt.NewRow()
                dr(0) = files.Name
                For f = 1 To Cols
                    dr(f) = Fields(f - 1)
                Next
                dt.Rows.Add(dr)

            Next


        Next

        Return dt

    Catch ex As Exception
        MessageBox.Show("Error is " + ex.ToString())
    End Try


End Function

End Class

Imports Task2.ReadBo

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        DataGridView1.AutoGenerateColumns = True
        Dim initialPath As String = "C:\Users\ng.yipeng\Desktop\Task2"
        FolderBrowserDialog1.SelectedPath = initialPath
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString()

            Me.DataGridView1.DataSource = ReadFile(TextBox1.Text)
        End If
    Catch ex As Exception
        MsgBox("Error: " & ex.Message)
    End Try
End Sub

Private Function ReadFile(p1 As String) As Object
    Throw New NotImplementedException
End Function

End Class

0 Answers
Related