0

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

Edited by Lorraine_1

3
Contributors
2
Replies
17
Views
2 Years
Discussion Span
Last Post by Reverend Jim
0

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?

Edited by ddanbe: typo

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.