vb.net 如何在 Visual Basic 2010 express 中读取 txt 文件并将结果显示在网格标题中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15859657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you read txt files and display the results in a grid headers in Visual Basic 2010 express?
提问by slyclam
This sounds so simple but I still can't find the answer to it after googling for a while. I have DataGridView on a form and it's called DataGridView1. I have two txt files (with single columns in each) data and I want to be able to display them as Row & Column headings (not as cell contents). For example, the two txt files are
这听起来很简单,但我在谷歌搜索了一段时间后仍然找不到答案。我在一个表单上有 DataGridView,它被称为 DataGridView1。我有两个 txt 文件(每个文件有单列)数据,我希望能够将它们显示为行和列标题(而不是单元格内容)。比如两个txt文件是
- C:\temp\sObj.txt
- C:\temp\sQue.txt
- C:\temp\sObj.txt
- C:\temp\sQue.txt
Now:
sObj.txt contains data a, b & c in a single column &
sQue.txt contains data d, e & f in a single column.
Now what I want looks like:
现在:
sObj.txt 在单列中包含数据 a、b 和 c,
sQue.txt在单列中包含数据 d、e 和 f。现在我想要的是:
a b c
d
e
f
Please help, I'm a beginner in programming & need this be done urgently.
请帮忙,我是编程的初学者,急需完成这项工作。
回答by jAC
As I already said in the comment section, you need a StreamReaderto read the lines of your textfile.
First of all you have to import the System.IO-namespace:
正如我在评论部分已经说过的,您需要StreamReader阅读文本文件的行。首先,您必须导入System.IO-namespace:
Import System.IO
So declare your StreamReaderwith the filepath to your textfile and read each line in it.
因此,StreamReader使用文本文件的文件路径声明您的文件并阅读其中的每一行。
Then you have to add the text of your StreamReader as a Column to your DataGridView. The first parameter is the ColumnName, the second is the ColumnHeading:
然后,您必须将 StreamReader 的文本作为列添加到 DataGridView。第一个参数是 ColumnName,第二个是 ColumnHeading:
Using reader As New StreamReader(@"YOURFILEPATHHERE")
DataGridView1.Columns.Add(reader.ReadLine(),reader.ReadLine())
End Using
So now you have added your columns. Adding your Rows is easier:
所以现在你已经添加了你的列。添加行更容易:
Simply read the lines and add your Rows with your String(linetext) as the parameter:
只需阅读行并添加您的行String(行文本)作为参数:
DataGridView1.Rows.Add(reader.ReadLine())
It's pretty easy and you could have done that with minimal research.
这很容易,你可以通过最少的研究来完成。
回答by Hiram
This answer was useful to me to build my own solution , I would like to share it with others;
这个答案对我构建自己的解决方案很有用,我想与他人分享;
Private Sub LoadColumnsInDGV(FileName As String)
Try
Dim sr As New System.IO.StreamReader(FileName)
Dim TxtNewLine As String
Dim IsFlagFound As Boolean = True
Dim NewColName As String
Dim SplitLine() As String
Do While sr.Peek() > -1
TxtNewLine = Trim(sr.ReadLine()) & vbNewLine
SplitLine = Split(TxtNewLine, vbTab)
If IsFlagFound Then
For i = 0 To SplitLine.Length - 1
NewColName = Trim(SplitLine(i))
NewColName = NewColName.Replace(vbNewLine, Nothing)
DataGridView1.Columns.Add(NewColName, NewColName)
Next
IsFlagFound = False
Else
DataGridView1.Rows.Add(SplitLine)
End If
Loop
sr.Close()
Catch ex As Exception
MsgBox("Error on Sub LoadColumnsInDGV " & vbCrLf & ex.Source & vbCrLf & ex.Message)
End Try
End Sub

