Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,

i have a csv table has 20 columns and i want to read them from col1 to col9.

What I have tried:

If
     Dim firstline As Boolean
     firstline = True
     While Not sr.EndOfStream
         If firstline = True Then

             Dim cols = sr.ReadLine.Split(";")
             For Each col1 In cols
                 If col1 = "first_col" Then

                     For Each col In cols
                         firstline = False
                         dt_table.Columns.Add(New DataColumn(col, GetType(String)))
                     Next
                 End If
             Next
         Else
             Dim data() As String = sr.ReadLine.Split(";")
             dt_table.Rows.Add(data.ToArray)
         End If
     End While
 End If
Posted
Updated 19-Sep-19 5:02am
v2
Comments
Maciej Los 18-Sep-19 15:48pm    
Seems you don't have an Excel file, but CSV file...

1 solution

Replace your For Each with a For and only iterate the first 9 elements.
Then use Array.Resize(T[], Int32) Method (System) | Microsoft Docs[^] to cut the data array down to just 9 items.
 
Share this answer
 
Comments
Maciej Los 19-Sep-19 2:01am    
5ed!

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