简体   繁体   English

ListView列

[英]ListView Columns

I have 2 columns in a ListView . 我在ListView有2列。 My "C:\\file.txt" looks like this: 我的“C:\\ file.txt”如下所示:

1;aaa 
2;bbb
3;ccc 
4;ddd 

and so on. 等等。 (each number and text in separate line) My code: (每个数字和文本在单独的行)我的代码:

FileStream spis = File.Open("C:\\file.txt", FileMode.Open, FileAccess.Read);
StreamReader yaRead = new StreamReader(spis);
string yaView = yaRead.ReadToEnd();
yaRead.Close();
spis.Close();
String[] yaArray = yaView.Split(new char[] {';'});
foreach (string ya in yaArray)
{
    listView1.Items.Add(ya);
}

It results 结果

1
aaa
bbb
(...)

...in first column and nothing in second column. ...在第一列中,在第二列中没有任何内容。 Please help me fix it. 请帮我修理一下。

You add columns to a list view via the SubItems property of a ListViewItem . 您可以通过ListViewItem的SubItems属性将列添加到列表视图中。

The simplest way is to do something like: 最简单的方法是执行以下操作:

ListViewItem newItem = new ListViewItem("1");
newItem.SubItems.Add("aaa");
listView1.Items.Add(newItem);

If we take your example, we can simply do something like the following: 如果我们举个例子,我们可以简单地做以下事情:

string[] yaArray = yaView.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); 

This will give you an array that looks like: 这将为您提供一个如下所示的数组:

[1;aaa]
[2;bbb]
[3;ccc]
[4;ddd]

We've split based on the new line rather than the semi-colon. 我们根据新行而不是分号进行拆分。

Then it's simply a matter of doing the following: 然后,只需执行以下操作:

foreach(string lineItem in yaArray)
{
  string[] listViewRow = lineItem.Split(new string[] { ";" }, StringSplitOptions.None); //Now we split on the semi colon to give us each item
  ListViewItem newItem = new ListViewItem(listViewRow[0]);
  newItem.SubItems.Add(listViewRow[1];
  listView1.Items.Add(newItem);
}

That should give you what you want. 这应该给你你想要的。

Please, add SUBitems to list 请将SUBitems添加到列表中

foreach(var line in File.ReadAllLines(@"C:\file.txt"))
{
  listView1.Items.Add(line.Split(';'));
}

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

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