Results 1 to 5 of 5

Thread: [RESOLVED] Populating datagridview from a .txt file

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Resolved [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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim lines = (From line In IO.File.ReadAllLines(filename) _
    5.                     Select line.Split(CChar(vbTab))).ToArray
    6.         For x As Integer = 0 To lines(0).GetUpperBound(0)
    7.             DataGridView1.Columns.Add(lines(0)(x), lines(0)(x))
    8.         Next
    9.         For x As Integer = 1 To lines.GetUpperBound(0)
    10.             DataGridView1.Rows.Add(lines(x))
    11.         Next
    12.     End Sub
    13.  
    14. End Class

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    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.

  5. #5
    Junior Member
    Join Date
    Dec 2017
    Posts
    16

    Re: Populating datagridview from a .txt file

    Quote Originally Posted by .paul. View Post
    here's an example using a tab delimited text file with a header row:

    this is the text file i used:



    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim lines = (From line In IO.File.ReadAllLines(filename) _
    5.                     Select line.Split(CChar(vbTab))).ToArray
    6.         For x As Integer = 0 To lines(0).GetUpperBound(0)
    7.             DataGridView1.Columns.Add(lines(0)(x), lines(0)(x))
    8.         Next
    9.         For x As Integer = 1 To lines.GetUpperBound(0)
    10.             DataGridView1.Rows.Add(lines(x))
    11.         Next
    12.     End Sub
    13.  
    14. 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
  •  



Click Here to Expand Forum to Full Width