Getting the Selected Item from a ListView

Greetings,

I need to access the selected item in a single selection list view.  But all I see is how to get the SelectedItemsListViewCollection.  Is this the collection which will contain the information I'll need?  I have used this for a multi-selection list, but am not sure how to proceed.

Thanks,
Bob
ba272Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
ListView.SelectedItems Property

When the MultiSelect property is set to true, this property returns a collection containing the items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing the only selected item in the ListView.
If no item is selected, collection is empty.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ravi SinghSenior Software EngineerCommented:
Hi Bob, if its single selection then you could use ListView's FocusedItem property (returns the currently selected ListViewItem):

ListViewItem selectedItem = this.listView1.FocusedItem;
0
AlexFMCommented:
I am not sure that FocusedItem gives correct result. It is possible to select item and then click on empty place. In this case there is not selection but there is focused item.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Ravi SinghSenior Software EngineerCommented:
Sorry, your right Alex... the FocusedItem actually returns the last selected item regardless of the current state of the listview,  using SelectedItems is the way to go, as Alex mentioned.
0
ba272Author Commented:
To use the SelectedItems ad Alex suggested, I've had to funky things, but can't get it to compile.

Is this the right way to go about getting the first item?  I've tried getting the first by using "[0]" but can't get it to compile.


OrderNode lastSelection = null;

lastSelection = (OrderNode)ordersLV.SelectedItems[0];

and I've tried this funky iterator without success.


while ( ListViewItem listViewItem in ordersLV.SelectedItems )
{
      lastSelection = (OrderNode)listViewItem;
      break;
}

What am I missing?

Thanks for the help,

Bob
0
AlexFMCommented:
Simplest way is:

foreach ( ListViewItem item in ordersLV.SelectedItems )
{
   ...
}

What is OrderNode? You need ListViewItem.

0
ba272Author Commented:
Thanks.  You're so right!  I was confused because when I used a ListBox, I added an object and overrode the ToString() method. Then when I had a selected item I just type-cast it back to the original object type.  Man was this an oversight!

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.