如何使用VB.net逐行读取文本文件并在DataGrid视图中显示 [英] How do I read a text file line by line using VB.net and displaying in a DataGrid View
问题描述
我是VB.net的新手,已经完成了家庭作业。我需要能够读取某些行并在DateGridView中显示它们。我已经能够将我的记事本.txt文件链接到DGV但是它读取整个文件而不是特定的行。我有4个按钮:btn1,btn2,btn3,btn4。我希望每个按钮显示文本文件中的相应行。经过过去一周的在线研究后,我仍然陷入困境。如果有人可以帮助我,我会非常感激。
文本文件(database.txt)
c1 c2 c3 >
一个1-1 1-2
两个2-2 2-3
三个3-2 3-3
四个4-2 4-3
Hi, 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 DateGridView. I have been able to link my notepad .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 want each button to show the respective lines in the text file. After researching on-line for the past week I'm still stuck. If anyone could help me I would really appreciate it.
Text File ("database.txt")
c1 c2 c3
one 1-1 1-2
two 2-2 2-3
three 3-2 3-3
four 4-2 4-3
Public Class Form1
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Dim lines = (From line In IO.File.ReadAllLines("database.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
End Class
推荐答案
使用 StreamReader.ReadLine方法 [ ^ ]逐行阅读。
这篇关于如何使用VB.net逐行读取文本文件并在DataGrid视图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!