I'm completely new to VB.net and have been given a homework assignment. I need to be able to read certain lines and display them in a DataGridView. I have been able to link my .txt file to the DGV however it reads the whole file as opposed to the specific line. I have 3 buttons: btn1, btn2, btn3. I want each button to show the respective lines in the text file. Eventually I plan to add Column 3 up to get a total so I need each line to remain in the datagrid view once the button is pressed. After researching on-line for the past week I'm still stuck. If anyone could help me I would really appreciate it.

Text File ("products.txt")

c1 c2 c3 *<-----Coloumn Headers

one 1.50 10

two 2.50 15

three 1.50 12

Public Class Form1

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click

    Dim lines = (From line In IO.File.ReadLines("products.txt") _
                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

Hi Lorraine 1, welcome here. :)

I have 3 buttons: btn1, btn2, btn3. I want each button to show the respective lines in the text file.
Could you please explain what you exactly mean?

You can add rows just by

For r = 0 To lines.GetUpperBound(0)
    DataGridView1.Rows.Add(lines(r))
Next

Just add a test at the top of the loop to select/ignore specific rows. To clear the grid you do

DataGridView1.Rows.Clear()

Other than that I'm not sure what your problem is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.