我的 listView 中有两列,我们称它们为 Column1 和 Column2。
是否可以仅将一堆项目添加到 column1,并使这些项目严格位于 column1 而不是 column2 下?
当我添加项目时,它会将一个项目添加到 Column1,然后是 Column2,我不希望这样。
我该怎么做呢?
这是我的一些测试代码。我用两个字符串制作了一个数组。我希望这两个字符串都位于 column1 下(每个都在它们自己的行中)而不是 column2 下。当我测试它时,它们仍然在 column1 和 column2 下,这是我不想要的。
string[] h = {"Hi","Hello"};
listViewGoogleInsight.Items.Add(new ListViewItem(h));
最佳答案
也许这会有所帮助:
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));
在您的代码中,您将一个字符串数组传递给 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);
}
关于c# - 只写入 listView 中的一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13242289/