【问题标题】:How to use Virtual mode in ListView?如何在 ListView 中使用虚拟模式?
【发布时间】:2011-07-21 21:56:21
【问题描述】:

我正在使用VirtualMode 来填充列,例如

List<ListViewItem> m_lstItem;


    private void Form1_Load(object sender, EventArgs e)
    {
        m_lstItem = Enumerable.Range(0, 100000).Select(X => new ListViewItem(new String[] { X.ToString(), (X + 1).ToString() })).ToList();
        listView1.VirtualListSize = m_lstItem.Count;
    }

    private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        e.Item = m_lstItem[e.ItemIndex];
    }

但我无法访问所选项目。在访问所选项目时,它会抛出类似Cannot access the selected items collection when the ListView is in virtual mode.

的错误

listViewVirtualMode中时,我如何从listView中获取所选项目

请帮我做这件事。

【问题讨论】:

    标签: c# winforms listview


    【解决方案1】:

    来自 MSDN:

    在虚拟模式下,Items 集合被禁用。尝试访问它会导致 InvalidOperationException。 CheckedItems 集合和 SelectedItems 集合也是如此。如果要检索选定或选中的项目,请改用 SelectedIndices 和 CheckedIndices 集合。

    【讨论】:

      【解决方案2】:

      Items 集合在虚拟模式下不能作为可迭代集合使用,但始终可以使用 Items(SelectedIndices(0)) 访问单个元素。我发现它也可以使用 FULLROWSELECT。该问题在同一站点的另一个页面上引用:Cannot access the selected items collection when the ListView is in virtual mode?

      【讨论】:

        【解决方案3】:

        由于某种原因,当我尝试使用 SelectedIndices 时,它们总是无效,可能是因为使用了 FULLROWSELECT。

        即使文档不明确,所选项目仍然可用。我在 ItemSelectionChanged 事件处理程序中发现它为 e.ItemIndex

        希望这可能对其他人有所帮助。

        【讨论】:

          猜你喜欢
          • 2010-09-25
          • 1970-01-01
          • 2023-03-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-18
          • 1970-01-01
          相关资源
          最近更新 更多