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 = OpenFileDialog1.FileNames(j)
Me.txt1.Text += FILENAME
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(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