我使用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.
当listView
在VirtualMode
中时,如何从它获取所选项目
请帮我做这件事。
发布于 2011-03-24 15:17:27
来自MSDN:
在虚拟模式下,将禁用Items集合。尝试访问它会导致InvalidOperationException。CheckedItems集合和SelectedItems集合也是如此。如果要检索选定或选中的项,请改用SelectedIndices和CheckedIndices集合。
发布于 2012-11-14 00:27:45
Items集合不能作为虚拟模式下的可迭代集合使用,但始终可以使用Items(SelectedIndices(0))访问单个元素。我发现使用FULLROWSELECT也可以。这个问题在这个网站的另一个页面上被引用:Cannot access the selected items collection when the ListView is in virtual mode?
发布于 2012-03-01 13:14:42
由于某些原因,当我尝试使用SelectedIndices时,它们总是无效的,可能是因为使用了FULLROWSELECT。
但是,即使文档不清楚,选定的项目也是可用的。我在ItemSelectionChanged事件处理程序中找到了它,名为e.ItemIndex
。
希望这能对其他人有所帮助。
https://stackoverflow.com/questions/5415877
复制相似问题