我的表单中有一个列表视图。
在我的文本文件中,我有这个:
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:40; 表示我想要放入列表视图第一列的时间
x; 0.0078; y; -0.0147; z; 0.9879; 是我想要创建三列以将X,Y,Z放在每列中以及相应列中的数据的位置
然后,下一行将在各自列的第2行
中他们被“;”
分开我如何在列表视图中显示它?
答案 0 :(得分:2)
试试这个
System.Windows.Forms.ListView listView = new System.Windows.Forms.ListView();
DateTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
X = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
Y = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
Z = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
SuspendLayout();
listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
DateTime,
X,
Y,
Z});
listView.GridLines = true;
listView.View = System.Windows.Forms.View.Details;
DateTime.Text = "DateTime";
X.Text = "X";
Y.Text = "Y";
Z.Text = "Z";
this.Controls.Add(this.listView);
StreamReader file = new StreamReader("filepath");
string sLine;
while ((sLine = file.ReadLine()) != null)
{
string[] sarr= sLine.Split(';');
StringBuilder sb = new StringBuilder(sarr[0]);
sb[sarr[0].IndexOf(':')] = ' ';
sarr[0] = sb.ToString().Replace(':', '.');
string[] sData = { sarr[0], sarr[2], sarr[4], sarr[6] };
ListViewItem item = new ListViewItem(sData);
listView.Items.Add(item);
}
之后,您可以将第一个数据添加到listView中,然后保持不变。并确保将listView视图属性设置为Details。
输出:
答案 1 :(得分:1)
以下是解决方案的新测试答案。
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();
}
这是输出(来自给定的数据):