Click here to Skip to main content
14,691,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a new Vb.net beginner. I wrote a program to view the multiple CSV files data in datagridview. I want to view multiple CSV files values in datagridview, however, I am only can view one value after the multiselect action. I found out that there is a FOR loop issue on loading the files' value. However, I can't figure out what should I change to the code.


Can someone help me to solve my issue by modifying my code?

Thanks a lot!!

What I have tried:

Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click

        OpenFileDialog1.Filter = "csv files|; *.csv"
        OpenFileDialog1.Title = "Select multiple CSV Files"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.RestoreDirectory = True

        If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Try
                'FileName in textbox
                filenames = OpenFileDialog1.FileNames(j)
                Me.txt1.Text += filenames

                'File Data
                For k As Integer = 0 To OpenFileDialog1.filenames.Count - 1

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

                        Dim lines = System.IO.File.ReadAllLines(filenames)

                        'datagridview
                        For Each str As String In lines

                            Dim thecolumns() As String = str.Split(",")
                            Dim newRow As DataRow = dt.NewRow
                            newRow("A") = thecolumns(0)
                            newRow("B") = thecolumns(1)
                            newRow("C") = thecolumns(2)
                            newRow("D") = thecolumns(3)
                            newRow("E") = thecolumns(4)
                            newRow("F") = thecolumns(5)
                            newRow("G") = thecolumns(6)
                            newRow("H") = thecolumns(7)
                            newRow("I") = thecolumns(8)
                            newRow("J") = thecolumns(9)
                            newRow("K") = thecolumns(10)
                            dt.Rows.Add(newRow)
                            DataGridView1.DataSource = dt

                        Next

                    Else
                        txt1.Text = "Error"
                    End If
                Next
            Catch ex As Exception
        'dialog box for error
        MsgBox("Error: " + ex.Message)
            End Try
            'j += 1

            'ListView
            For Each filenames As String In OpenFileDialog1.FileNames
                ListBox1.Items.AddRange(IO.File.ReadAllLines(filenames))
            Next

            'dialogbox
            MsgBox(OpenFileDialog1.FileNames.Count & " files had been loaded")

        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'datatable format
        With dt
            .Columns.Add("A", System.Type.GetType("System.String"))
            .Columns.Add("B", System.Type.GetType("System.String"))
            .Columns.Add("C", System.Type.GetType("System.String"))
            .Columns.Add("D", System.Type.GetType("System.String"))
            .Columns.Add("E", System.Type.GetType("System.String"))
            .Columns.Add("F", System.Type.GetType("System.String"))
            .Columns.Add("G", System.Type.GetType("System.String"))
            .Columns.Add("H", System.Type.GetType("System.String"))
            .Columns.Add("I", System.Type.GetType("System.String"))
            .Columns.Add("J", System.Type.GetType("System.String"))
            .Columns.Add("K", System.Type.GetType("System.String"))
        End With

    End Sub

    'Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    '    Dim sr As New IO.StreamReader(filenames, System.Text.Encoding.Default)

    '    Dim sline As String = ""

    '    Do
    '        sline = sr.ReadLine
    '        If sline Is Nothing Then Exit Do
    '        Dim thecolumns() As String = sline.Split(",")
    '        Dim newRow As DataRow = dt.NewRow
    '        newRow("A") = thecolumns(0)
    '        newRow("B") = thecolumns(1)
    '        newRow("C") = thecolumns(2)
    '        newRow("D") = thecolumns(3)
    '        newRow("E") = thecolumns(4)
    '        newRow("F") = thecolumns(5)
    '        newRow("G") = thecolumns(6)
    '        newRow("H") = thecolumns(7)
    '        newRow("I") = thecolumns(8)
    '        newRow("J") = thecolumns(9)
    '        newRow("K") = thecolumns(10)
    '        dt.Rows.Add(newRow)
    '        DataGridView1.DataSource = dt

    '    Loop
    '    sr.Close()

    'End Sub
End Class
Posted
Updated 20-Oct-20 3:59am
v2

1 solution

You are setting the datasource of your datagridview after each individual line that you have added see where you have
dt.Rows.Add(newRow)
DataGridView1.DataSource = dt
Don't do that. Completely fill your datatable first, then assign it to the DataGridView.Datasource e.g. at
             Next
DataGridView1.DataSource = dt
            Catch ex As Exception
.. etc

You need to understand how to debug your code - things like this become clearer when you can see what is going on
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900