如何在.NET 2.0 ListView 中全选/全选?
在不使用以下内容的情况下选择列表视图中的所有项目或不选择任何项目的好方法是什么:
foreach (ListViewItem item in listView1.Items)
{
item.Selected = true;
}
或者
foreach (ListViewItem item in listView1.Items)
{
item.Selected = false;
}
我知道底层 Win32 列表视图公共控件支持 LVM_SETITEMSTATE 消息,您可以使用它来设置选定状态,并通过传递 -1 作为索引,它将应用于所有项目。 我不想向恰好位于 .NET Listview 控件后面的控件发送 PInvoking 消息(我不想成为一个糟糕的开发人员并依赖未记录的行为 - 当他们将其更改为完全托管的 ListView 类时)
凹凸
伪受虐狂有< strong>SelectNone 情况:
ListView1.SelectedItems.Clear();
现在只需要 SelectAll 代码
What is a good way to select all or select no items in a listview without using:
foreach (ListViewItem item in listView1.Items)
{
item.Selected = true;
}
or
foreach (ListViewItem item in listView1.Items)
{
item.Selected = false;
}
I know the underlying Win32 listview common control supports LVM_SETITEMSTATE message which you can use to set the selected state, and by passing -1 as the index it will apply to all items. I'd rather not be PInvoking messages to the control that happens to be behind the .NET Listview control (I don't want to be a bad developer and rely on undocumented behavior - for when they change it to a fully managed ListView class)
Bump
Pseudo Masochist has the SelectNone case:
ListView1.SelectedItems.Clear();
Now just need the SelectAll code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇,这很旧了... :D
SELECT ALL
SELECT INVERSE
BeginUpdate
和EndUpdate
用于禁用/启用控件在更新其内容时重绘...我认为它会更快地选择所有内容,因为它只会刷新一次,而不是listView.Items.Count
次。Wow this is old... :D
SELECT ALL
SELECT INVERSE
BeginUpdate
andEndUpdate
are used to disable/enable the control redrawing while its content is being updated... I figure it would select all quicker, since it would refresh only once, and notlistView.Items.Count
times.无论如何,要么
or
应该可以解决 select none 的问题。
Either
or
should do the trick for select none, anyway.