views:

53

answers:

1

Hello,

How can I check if no items "At all" selected from listview?

Thanks.

+7  A: 
if( myListView.SelectedItems == null || myListView.SelectedItems.Count == 0 )
{
}

See ListView..::.SelectedItems Property for more info.

EDIT: As per the MSDN documentation:

If no items are currently selected, an empty ListView..::.SelectedListViewItemCollection is returned.

So the null check is not needed in this case and you can simply do:

if( myListView.SelectedItems.Count == 0 )
{
}
Winston Smith
Are you sure you have to check for null?
tanascius
I think testing for null should not be necessary. Would be really ugly then.
OregonGhost
msdn: "If no items are currently selected, an empty ListView.SelectedListViewItemCollection is returned."
tanascius
@tanascius I wasn't sure off the top of my head, so I left the null check in. You're correct though, and I've updated the answer accordingly.
Winston Smith
At first I was not sure, either ^^ but you already got my +1
tanascius
Just remember that if the `ListView` is in virtual mode, you cannot access the `SelectedItems` collection. It's better to always use the `SelectedIndicies` collection, since that is always available.
Grammarian