Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 4 buttons: btn1, btn2, btn3, btn4.

I added a new button to the form that loads the data from the text file, parses it to a datatable and sets the DataGridView1.DataSource to that DataTable. The second method then creates a new datatable and imports the specified row from the main datatable and shows it in the DGV.

I need to be able to press btn1 and have the data displayed in the DGV and if btn2 is pressed afterwards the data would be displayed in the row under it etc.

Any help would be greatly appreciated.

VB
Private txtDataTable As DataTable Private Sub loadFileBtn_Click(sender As Object, e As EventArgs) Handles loadFileBtn.Click txtDataTable = New DataTable("txtContents")

Dim txtContents As String()
Try
    txtContents = IO.File.ReadAllLines("database.txt")
Catch ex As Exception
    MsgBox(ex.Message)
    Return
End Try

Dim txtLines As New List(Of String())
txtContents.ToList().ForEach(Sub(x) txtLines.Add(x.Split(CChar(vbTab))))

If txtLines.Count > 0 Then
    txtLines.Item(0).ToList.ForEach(Sub(x) txtDataTable.Columns.Add(New DataColumn(x.ToString)))
    txtLines.RemoveAt(0)
End If

If txtLines.Count > 0 Then
    txtLines.ToList.ForEach(Sub(x) txtDataTable.Rows.Add(x.ToArray))
End If
DataGridView1.DataSource = txtDataTable
End Sub


Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn1.Click,
btn2.Click, btn3.Click, btn4.Click
If txtDataTable Is Nothing Then Return

Dim rowIndex As Integer
If Integer.TryParse(DirectCast(sender, Button).Name.Replace("btn", String.Empty), rowIndex) Then
    rowIndex -= 1
Else
    Return
End If

Dim TempTable As DataTable = txtDataTable.Clone
If rowIndex < txtDataTable.Rows.Count Then
    TempTable.ImportRow(txtDataTable.Rows(rowIndex))
<pre lang="vb">End If

DataGridView1.DataSource = TempTable</pre>



<i>Output
c1  c2  c3      <=====Required Output
One 50  80
Two 60  80
Three   60  70


c1c2c3              <=====Current Output
One5080
Two6080
Three6070           </i>
Posted
Updated 9-Mar-15 3:27am
v2
Comments
Richard MacCutchan 9-Mar-15 9:50am    
You need to split your text lines into separate fields before adding them to the grid. And writing code like txtLines.ToList.ForEach(Sub(x) txtDataTable.Rows.Add(x.ToArray)) makes it really difficult to debug when things go wrong.
Member 11494437 9-Mar-15 10:04am    
It's a TAB delimited text file, isnt that splitting the text lines into seperate fields?
Richard MacCutchan 9-Mar-15 10:38am    
I don't know, it's your code. Try stepping through it with your debugger so you can see exactly what is happening at each point in the program.

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