Click here to Skip to main content
14,691,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a new VB.net beginner. Currently, I had found some issues with displaying multiple CSV files' value after multiselect action. I had done the multiselect action, and able to multiselect on several CSV files. However, I am unable to display several CSV files' values in the DataGridView. There is only one CSV file value is displayed in the DataGridView.

Below is the coding that had done.

Can someone help me to view and solve my issue? I don't know what is wrong with my code.

Thank you very much!

What I have tried:

1. Button To Choose File
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
 OpenFileDialog1.FilterIndex = 2

 If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
     For j = 0 To OpenFileDialog1.FileNames.GetUpperBound(0)
         Try
             'FileName in textbox
             FILENAME = OpenFileDialog1.FileNames(j)
             Me.txt1.Text += FILENAME

             'File data
             For k As Integer = 0 To OpenFileDialog1.FileNames.Count
                 Dim textLine As String = ""
                 Dim sp() As String
                 If System.IO.File.Exists(FILENAME) = True Then

                     Dim Contents = IO.File.ReadAllLines(FILENAME).ToString

                     Dim sr As New System.IO.StreamReader(FILENAME)

                     dt = BuildDataTable(FILENAME, ";")
                     DS = New DataSet()
                     DS.Tables.Add(dt)
                     DataGridView1.DataSource = dt

                     Do
                         Do While sr.EndOfStream And sr.Peek() <> -1
                             textLine = sr.ReadLine()
                             sp = Split(textLine, ";")
                             Me.DataGridView1.Rows.Add(sp)

                         Loop
                     Loop
                 Else
                     txt1.Text = "Error"
                 End If
             Next
         Catch ex As Exception


         End Try

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

 End If
End Sub


2. Build Datatable
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal separator As Char) As DataTable Dim dt As DataTable = New DataTable("Table") Dim dr As DataRow Dim fieldValues As String() Dim f As IO.File = Nothing Dim j As Integer

 'Dim sr As New IO.StreamReader(fileFullPath, System.Text.Encoding.Default)
 Dim sr As New IO.StreamReader(FILENAME, System.Text.Encoding.Default)

 For k As Integer = 0 To OpenFileDialog1.FileNames.Count
     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)

     Loop
     sr.Close()
 Next
End Function


3. Form properties

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 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


4. Button to display data
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim sr As New IO.StreamReader(FILENAME, System.Text.Encoding.Default) Dim sline As String = "" Dim values As String() Dim n As Integer = 0

 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 19-Oct-20 3:36am
v2

1 solution

You are building a new datatable for each file, which is ok but you are making the datasource for the DataGridView that datatable.
Try moving
DS = New DataSet()
outside of the loop that iterates through your files. Add a datatable into that Data set for each file and then use the DataSet as the datasource to your DataGridView
   

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