在C#中将文本文件数据的内容显示到ListView [英] Display contents of Textfile data to ListView in C#
问题描述
我的表单中有一个列表视图.
I have a listview in my form.
在我的文本文件中,我有这个:
In my text file I have this:
24-7-2017:13:44:40; x; 0.0078; y; -0.0147; z; 0.9879;
24-7-2017:13:44:40;x;0.0078;y;-0.0147;z;0.9879;
24-7-2017:13:44:41; x; 0.0069; y; -0.0069; z; 1.0104;
24-7-2017:13:44:41;x;0.0069;y;-0.0069;z;1.0104;
24-7-2017:13:44:40; 表示我要放入列表视图第一列的时间
24-7-2017:13:44:40; represents the time where I want to put in the first column of the listview
x; 0.0078; y; -0.0147; z; 0.9879; 是我要创建的三列,分别将X,Y,Z放入各列,并将数据放入各列
x;0.0078;y;-0.0147;z;0.9879; is where I want to create three columns to put the X,Y,Z in the each column and the data in the respective column
然后下一行将在其相应列的第2行中
the next line will then be in row 2 in their respective column
它们之间用;"分隔
如何在列表视图中显示它?
How I go about displaying it in the listview?
推荐答案
这是该解决方案的新测试答案.
Here is the new tested answer with the solution.
public Form1()
{
InitializeComponent();
//read the file
System.IO.StreamReader file =
new System.IO.StreamReader("yourFileName.txt");
//set list view in details mode
listView1.View = View.Details;
//Set columns in listview
listView1.Columns.Add("Date Time");
listView1.Columns.Add("X");
listView1.Columns.Add("Y");
listView1.Columns.Add("Z");
string line = "";
//read text file line by line.
while (( line = file.ReadLine()) != null)
{
var itemMC = new ListViewItem(new[] { line.ToString().Split(';')[0].ToString(), line.ToString().Split(';')[2].ToString(),
line.ToString().Split(';')[4].ToString(), line.ToString().Split(';')[6].ToString() });
listView1.Items.Add(itemMC);
}
file.Close();
}
以下是输出(来自相关数据):
Here is the output(from the given data in question) :
这篇关于在C#中将文本文件数据的内容显示到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!