[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ListView SelectedIndexChanged trouble getting SelectedItems

Posted on 2006-06-03
Medium Priority
Last Modified: 2007-12-19

I'm handleing the correct event when the user changes the selected item, but when I go and try to get that item, I'm getting null.  I use

private ListViewItem GetSelection( ListView listView )
    foreach ( ListViewItem listViewItem in listView.SelectedItems )
        return listViewItem;
    return null;

to get the one and on y choice in my single selection ListView.  I know there's a better way to do this, and if you have it I'd love to see it.  But the problem is that there the foreach does not get entered into, even thoiugh I only call this function when the user has selected an item in the ListView.  Can anybody suggest a solution?

Question by:ba272
LVL 11

Accepted Solution

vo1d earned 2000 total points
ID: 16826627
why dont you make something like this:

internal ListViewItem _selectedItem; //<-- classmember field
//eventhandler for your listviews
private void ListViewSelectedIndexChanged(object sender, EventArgs e)
  ListView listView = sender as ListView;
  this._selectedItem = (listView.SelectedItems.Count > 0 ? listView.SelectedItems[0] : null);

_selectedItem is null or the selected listviewitem.

Author Comment

ID: 16939083
thanks. works great.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question