繁体   English   中英

从文本文件读取后显示数据-C#,Visual Studio

[英]Displaying Data after reading from text file - C#,Visual Studio

我有一个文本文件,其数据可能类似于:

用户名

地址

邮编

电话号码

文本文件的前四个元素属于一个用户,接下来的四个元素属于下一个用户,等等。我想从文本文件中读取内容,并显示每个用户的数据。 读取和区分每个用户的数据很好。 问题是我将如何显示数据? 我想将数据显示为表格或沿行显示的内容,其中每个用户都有一行。 假设我要显示类似的数据;

名称-地址-邮政编码-电话号码

Matt-15 The-PO30 78-088997655

迈克-16 The-PO31 78-088998955

如果我使用的是数据库,我想您可以使用GridView轻松显示它,从文本文件读取后是否仍然可以显示它?

非常感谢,迈克

编辑:我复制了您提供的代码,将gridView拖到页面上。 将其ID更改为dataGridView1

DataTable table = new DataTable();
   table.Columns.Add("UserName", typeof(string));
        table.Columns.Add("Address", typeof(string));
        table.Columns.Add("PostCode", typeof(string));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("LastName", typeof(string));
        dataGridView1.DataSource = table;
    table.Rows.Add(Label8.Text, Label4.Text, Label5.Text, Label6.Text, Label7.Text);

您可以将数据加载到DataTable中,然后将gridview绑定到DataTable,就像它来自数据库一样。 像这样:

        DataTable table = new DataTable();
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Address", typeof(string));
        table.Columns.Add("Postcode", typeof(string));
        table.Columns.Add("PhoneNumber", typeof(string));
        table.Rows.Add("Matt", "15 The", "PO30 78", "088997655");
        dataGridView1.DataSource = table;
        dataGridView1.DataBind();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM