-
Aug 3rd, 2010, 01:25 PM
#1
Thread Starter
Member
[RESOLVED] Populating datagridview from a .txt file
Hello,
I am trying to populate a datagridview from a .txt file but I don’t know how I can do this. Text file will include data copied and pasted from excel. So the text separator will be tab. If you can show me some sample code I’ll be so glad. Thank you.
-
Aug 3rd, 2010, 01:57 PM
#2
Re: Populating datagridview from a .txt file
here's an example using a tab delimited text file with a header row:
this is the text file i used:
c1 c2 c3
one 1-1 1-2
two 2-2 2-3
three 3-2 3-3
four 4-2 4-3
five 5-2 5-3
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines = (From line In IO.File.ReadAllLines(filename) _
Select line.Split(CChar(vbTab))).ToArray
For x As Integer = 0 To lines(0).GetUpperBound(0)
DataGridView1.Columns.Add(lines(0)(x), lines(0)(x))
Next
For x As Integer = 1 To lines.GetUpperBound(0)
DataGridView1.Rows.Add(lines(x))
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 3rd, 2010, 02:01 PM
#3
Re: Populating datagridview from a .txt file
aliefeozkan, you could use the Jet engine to connect to the excel file itself then simply bind that to the DataGridView. Here's the Oledb connection string Excel Connection String Samples - ConnectionStrings.com
-
Aug 5th, 2010, 03:38 PM
#4
Thread Starter
Member
Re: Populating datagridview from a .txt file
.paul. thank you so much. that was what I was looking for. Thanks.
JuggaloBrotha thanks for reply as well, I will consider your suggestion.
-
Dec 24th, 2017, 05:45 AM
#5
Junior Member
Re: Populating datagridview from a .txt file
Originally Posted by .paul.
here's an example using a tab delimited text file with a header row:
this is the text file i used:
vb Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lines = (From line In IO.File.ReadAllLines(filename) _ Select line.Split(CChar(vbTab))).ToArray For x As Integer = 0 To lines(0).GetUpperBound(0) DataGridView1.Columns.Add(lines(0)(x), lines(0)(x)) Next For x As Integer = 1 To lines.GetUpperBound(0) DataGridView1.Rows.Add(lines(x)) Next End Sub End Class
How can I write and retreive using delimited text files ???
is there an appropriate link ?
I would like to store the following information about my customers :
name
Age
Weight
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|