How to select All / SelectNone in .NET 2.0 ListView?

What is a good way to select all or not select items in a list without using:

foreach (ListViewItem item in listView1.Items) { item.Selected = true; } 

or

 foreach (ListViewItem item in listView1.Items) { item.Selected = false; } 

I know that the basic common Win32 list control supports the LVM_SETITEMSTATE message , which you can use to set the selected state, and passing -1 as the index will apply to all elements. I would rather not send PInvoking messages to the control that is behind the .NET Listview control (I don't want to be a bad developer and rely on undocumented behavior) when they change it to a fully managed ListView class)

Bump

Pseudo Masochist has a SelectNone case:

 ListView1.SelectedItems.Clear(); 

Now I just need SelectAll code

+4
source share
2 answers

Wow this is old ...: D

CHOOSE ALL

  listView1.BeginUpdate(); foreach (ListViewItem i in listView1.Items) { i.Selected = true; } listView1.EndUpdate(); 

SELECT INVERSE

  listView1.BeginUpdate(); foreach (ListViewItem i in listView1.Items) { i.Selected = !i.Selected; } listView1.EndUpdate(); 

BeginUpdate and EndUpdate are used to disable / enable redrawing of controls while its contents are being updated ... I believe that it will select faster as it is updated only once, not listView.Items.Count times.

+2
source

Or

 ListView1.SelectedItems.Clear(); 

or

 ListView1.SelectedIndices.Clear(); 

should do the trick to select none, anyway.

+4
source

Source: https://habr.com/ru/post/1276823/


All Articles