c# - 只写入 listView 中的一列?

标签 c# winforms listview

我的 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/

相关文章:

c# - 对下面的应用程序进行表单点击 "fall through"

java - Android - ListView 项上的 OnClickListener 仅影响最后一行

c# - 当对集合的引用未更改时,我应该返回集合吗?

javascript - TinyMCE 4.7.4 不适用于包含查询字符串参数的 URL

Windows 服务上的 C# & NHibernate "Could not compile the mapping document"

c# - Winforms 应用程序中 RichTextBox 中的当前行号和列号

c# - 从 C# 中的同一个流中读取多次

c# - 找不到 System.Windows.Point

带有 simplecursor 适配器的 Android ListView 使应用程序崩溃(未显示 ANR)

c# - WPF - 将列表添加到树中的好方法