• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

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
0
ba272
Asked:
ba272
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now