3

我的 listView 中有两列,我们称它们为 Column1 和 Column2。

是否可以仅将一堆项目添加到 column1,并使其仅在 column1 下而不是 column2 下?

当我添加项目时,它会将一个项目添加到 Column1,然后是 Column2,我不希望这样。

我该怎么做呢?

这是我的一些测试代码。我用两个字符串制作了一个数组。我希望这两个字符串都放在 column1 下(每个都在自己的行中)而不是 column2 下。当我测试它时,它们仍然在 column1 AND column2 下,这是我不想要的。

string[] h = {"Hi","Hello"};
listViewGoogleInsight.Items.Add(new ListViewItem(h));
4

1 回答 1

1

也许这会有所帮助:

string[] h = {"Hi","Hello"}; 
foreach(string s in h) //this in case when you have more that two strings in you array
   listViewGoogleInsight.Items.Add(new ListViewItem(s));

在您的代码中,您将一个字符串数组传递给 a 的构造函数,该构造函数ListViewItem正在创建“Hello”的一个 ListViewSubitem。您应该分别添加每个字符串。

编辑:在您要求仅添加到 column2 之后,您可以这样做。您必须将空字符串传递给第一列,因为您需要为第一列创建一个 ListViewItem 并为每个其他列创建一个额外的 ListViewSubitems。

    string[] h = { "Hi", "Hello" };
int count=0;
                foreach (string s in h) //this in case when you have more that two strings in you array
                {
                    ListViewItem lvi = listView1.Items[count++];
                    lvi.SubItems.Add(s);
                    listView1.Items.Add(lvi);
                }
于 2012-11-06T00:09:25.580 回答