Read the text file first and fetch the columns correspondingly into a DataTable (other types of collections are feasible as well).
Bind DataTable to the GridView control.
My solution below focus on reading contents from the text file and render them inside a GridView Control.
The actually implementation depends on the structure of your text files and you should still need to provide some validations on the text file to avoid unexpected inputs.
More details, you could refer to below codes:
.aspx Page:
<form id="form1" runat="server">
<div>
<h3>Display Txt from Server Side</h3>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
<div><h3>Upload Txt and Display</h3>
<asp:FileUpload ID="Uploader1" runat="server" />
<asp:Button ID="UploadBtn" runat="server" OnClick="UploadBtn_Click" Text="Submit And Display" />
<asp:GridView ID="GridView2" runat="server"></asp:GridView>
</div>
</form>
Code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridView1()
End If
End Sub
Protected Sub BindGridView1()
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Property", GetType(String))
Dim txtPath As String = "C:\Path\To\File\testData.txt"
Dim lines As String() = File.ReadAllLines(txtPath)
For Each line As String In lines
Dim cols = line.Split(" "c)
Dim dr As DataRow = dt.NewRow()
For i As Integer = 0 To 3 - 1
dr(i) = cols(i)
Next
dt.Rows.Add(dr)
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub BindGridView2()
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Property", GetType(String))
Dim file As HttpPostedFile = Uploader1.PostedFile
Using reader As StreamReader = New StreamReader(file.InputStream)
Dim line As String = reader.ReadLine()
While line IsNot Nothing
Dim cols = line.Split(" "c)
Dim dr As DataRow = dt.NewRow()
For i As Integer = 0 To 3 - 1
dr(i) = cols(i)
Next
dt.Rows.Add(dr)
line = reader.ReadLine()
End While
End Using
GridView2.DataSource = dt
GridView2.DataBind()
End Sub
Protected Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
BindGridView2()
End Sub
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Thank you, but can I put the ID in value and Name in value and Property in value
Could you please specify the problem?
Sorry that I do not get the question very clear. You orginal question is
I have a text file that is sperated by spaces into 3 collumns I wont (want I guess) to read this in and then display it in a web page in a table is this possible?
How do you want to "put ID in value and Name in value and Property in value"?
The code just assigns variables "ID", "Name" and "Property" to strings "a", "b" and "c".
If you want to rewrite values to the .txt file, you could refer to below codes and documentation.
Construct a string array or one string and then write it to the .txt file.
// Example #1: Write an array of strings to a file.
// Create a string array that consists of three lines.
string[] lines = { "First line", "Second line", "Third line" };
// WriteAllLines creates a file, writes a collection of strings to the file,
// and then closes the file. You do NOT need to call Flush() or Close().
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
// Example #2: Write one string to a text file.
string text = "A class is the most powerful data type in C#. Like a structure, " +
"a class defines the data and behavior of the data type. ";
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file. You do NOT need to call Flush() or Close().
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
1 Points
2 Posts
ASP.NET VB Read in TXT file and Display in Table
Dec 13, 2020 11:13 PM|abdullah1122|LINK
ASP.NET VB Read in TXT file and Display in Table Gridview
Hi Guys and Gals,
I have a question that I'm guessing is straigh forward but I might be making a bit of a mess of this.
I have a text file that is sperated by spaces into 3 collumns I wont to read this in and then display it in a web page in a table is this possible?
Contributor
2640 Points
795 Posts
Re: ASP.NET VB Read in TXT file and Display in Table
Dec 14, 2020 03:15 AM|Sean Fang|LINK
Hi abdullah1122,
It is definitely possible.
Steps:
My solution below focus on reading contents from the text file and render them inside a GridView Control.
The actually implementation depends on the structure of your text files and you should still need to provide some validations on the text file to avoid unexpected inputs.
More details, you could refer to below codes:
.aspx Page:
Code behind:
Text file content:
Demo:
Hope this helps.
Best regards,
Sean
Member
1 Points
2 Posts
Re: ASP.NET VB Read in TXT file and Display in Table
Dec 15, 2020 06:40 AM|abdullah1122|LINK
Thank you, but can I put the ID in value and Name in value and Property in value
Example
Dim a as string = ID
Dim b as string = name
Dim c as string = Property
Contributor
2640 Points
795 Posts
Re: ASP.NET VB Read in TXT file and Display in Table
Dec 15, 2020 11:14 AM|Sean Fang|LINK
Hi abdullah1122,
Could you please specify the problem?
Sorry that I do not get the question very clear. You orginal question is
How do you want to "put ID in value and Name in value and Property in value"?
The code just assigns variables "ID", "Name" and "Property" to strings "a", "b" and "c".
If you want to rewrite values to the .txt file, you could refer to below codes and documentation.
More examples, you could refer to:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file
Hope this helps.
Best regards,
Sean