-1

我正在使用具有两列的ListView 。

要添加新项目(到零列),我正在使用:

listView1.Items.Add("Hello")

这导致Hello出现在索引最低的列中。但是,我还想添加行号

我正在尝试这个:

        for (int c = 0; c < listView1.Items.Count; c++)
        {
            listView1.Items[c].SubItems.Clear();
            listView1.Items[c].SubItems.Add(c.ToString());
        }

我认为,问题在于主要项目被清除,即使我只是在尝试clear()子项目(即第二列中的条目)。

所以基本上我的问题是: 我如何单独编辑列条目以便能够显示行/行号?

4

2 回答 2

0

SubItems 中的第一项(索引 0)实际上是左列中的主要项。

如果您已经添加了这样的子项目,listViewItem.SubItems.Add(string.Empty);那么您可以像这样编辑该子项目:

    for (int c = 0; c < listView1.Items.Count; c++)
    {
        listView1.Items[c].SubItems[1].Text = c.ToString();
    }
于 2015-05-28T19:11:08.017 回答
-1

我可能已经解决了...

可能一旦SubItem添加了 a ,主要项目就会移动到SubItem[0]

以下代码似乎对我有用,但我不确定是否存在更好的方法。

        for (int c = 0; c < listView1.Items.Count; c++)
        {
            listView1.Items[c].SubItems[0].Text = listView1.Items[c].Text;
            listView1.Items[c].SubItems.Add(c.ToString());
            listView1.Items[c].SubItems[1].Text = c.ToString();
        }
于 2015-05-28T19:38:19.413 回答