我有一个文本文件,其中的数据可能类似于:
用户名
地址
邮编
******中国
文本文件的前四个元素属于一个用户,下一个用户属于下一个用户等。我想从文本文件中读取,并显示每个用户的数据。读取和区分每个用户的数据很好。 问题是如何显示数据?我希望将数据显示为表格或其他内容,每个用户都有一行。说我想显示像;
这样的数据姓名 - 地址 - 邮政编码 - 电话号码
马特 - 15 The - PO30 78 - 088997655
迈克 - 16 The - PO31 78 - 088998955
如果我使用的是数据库,我猜你可以用GridView轻松地显示它,无论如何在从textfile读取后显示它?
非常感谢, 麦克
编辑: 我复制了你给的代码,将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);
答案 0 :(得分:1)
您可以将数据加载到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();