Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file named test.txt and I want to load it on my datagrid view.
Inside the text file:

Acct. Number, Name, Address, Need
0012123123, Jef, Canada, Water
0001231542, Mel, Australia, Fire
..etc.

those data should load in my datagrid>>
Account Number | Name | Address | Need |


Is there anyone can teach me to do that...tnx
Posted

VB
'Read the data from text file
       Dim textData As String() = System.IO.File.ReadAllLines(Me.txtSource.Text)
       Dim headers As String() = textData(0).Split(","c)

       'Create and populate DataTable
       Dim dataTable1 As New DataTable

       For Each header As String In headers
           dataTable1.Columns.Add(header, GetType(String), Nothing)
       Next
       For i As Integer = 1 To textdata.length - 1
           dataTable1.Rows.Add(textData(i).Split(","c))
       Next

       'Set the DataSource of DataGridView to the DataTable
       Me.DataGridView1.DataSource = dataTable1
       Me.Controls.Add(dataGridView1)


======================================================================
I tried to study this codes.. Can anayone help me to understand the for loop..
 
Share this answer
 

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