When I am reading a csv file to dataTable and populating a Datagridview I am getting
problem that one blank row inserted into datagridview why? what wrong with my code
please somebody help me. below my code
Private Sub ReadCsv()
Dim myTable As DataTable = New DataTable
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
'Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields there are.
myReader = IO.File.OpenText("C:\Test2.csv")
fieldValues = myReader.ReadLine().Split(",")
'Create data columns accordingly
' For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Name")) '("Field" & i))
myTable.Columns.Add(New DataColumn("Last Price"))
myTable.Columns.Add(New DataColumn("Date"))
myTable.Columns.Add(New DataColumn("Time"))
myTable.Columns.Add(New DataColumn("Change in RS"))
myTable.Columns.Add(New DataColumn("Open"))
myTable.Columns.Add(New DataColumn("High"))
myTable.Columns.Add(New DataColumn("Low"))
myTable.Columns.Add(New DataColumn("Vol"))
' Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(",")
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return 'New DataTable("Empty")
Finally
myReader.Close()
End Try
DataGridView2.DataSource = myTable
Return 'myTable
End Sub