Windows Develop Bookmark and Share   
 index > Windows Forms General > How do I get text of selected items in listview control?
 

How do I get text of selected items in listview control?

I have a listview control that contains a number of selected items. I am struggling to get the text of the items which are selected in the list.

The column names are ID, FirstName, LastName, and ContactNumber. I have some 10 people in the list. For example, how can I get the IDs of all the items which I have selected with the mouse. In other words, I want to have the ID columns of all the horizontal rows I have selected with the mouse.

I'm getting really confused with "SelectedListViewItemCollection", "ListViewSubItemCollection", IEnumerator, etc. At first I tried going through a SelectedListViewItemCollection using a "foreach" statement, but when I looked for examples it appeared that I should be using Enumerators and both a ListViewItemCollection and ListViewSubItemCollection. Could anyone tell me how I can display the text (ID column or any) of all the rows that I have selected?
MigrationUser 1  Thursday, June 12, 2003 7:34 PM
Eugene,
Use the SelectedItems property to return a collection of selected items.
The results will depnd on whether you have allowed multi slect or not.

hth,
Todd
MigrationUser 1  Friday, June 13, 2003 12:20 PM
A couple things to know about the quirky ListView control (examples in C#):

Upon adding an Item to the ListView, that Item mysteriously also gets a new SubItem.  So an Item will never have SubItem.Count == 0.  In your case, Item.SubItem[0] will be in the column Columns[0] (with ColumnHeader ID, I believe you said).  Item.SubItem[1] will be under Columns[1] (with ColumnHeader FirstName), etc.

Secondly, ListViews have two relevant collections: Items and Columns.  Items is a collection of ListViewItems while Columns is a collection of ColumnHeaders.  ListViews also have a collection called SelectedItems, a subset of Items (so it, too, holds ListViewItems).  Each instantiated ListViewItem will have its own SubItems collection (whose first item, as we said, actually takes the text of the item itself), which is a collection of ListViewSubItems.  The catch: ListViewSubItem is a sub-class of ListViewItem, which makes it illegal to write:


ListViewSubItem lvsi = ...;


You must instead write:


ListViewItem.ListViewSubItem lvsi = ...;


Ok, so back to your question.  Let's call your ListView listView1.  Assuming that ID is your first ColumnHeader (ie, listView1.Columns[0]), you can get all of the selected IDs with the following code:


foreach (ListViewItem lvi in listView1.SelectedItems) {
   MessageBox.Show(lvi.ToString()); // or just access the ID with lvi.Text
}


However, this only works because ID is the first column.  If ID is the second column (Columns[1]), you will have to do something like this:


foreach (ListViewItem lvi in listView1.SelectedItems) {
   ListViewItem.ListViewSubItem lvsi = lvi.SubItems[1];  // extra step for clarity
   MessageBox.Show(lvsi.ToString());
}


To make your code even more general, first find the index of the column with header "ID" like this:


int index;
foreach (ColumnHeader colhead in listView1.Columns) {
   if(colHead.Text == "ID") index = colHead.Index;
}



Tom
MigrationUser 1  Friday, June 20, 2003 6:14 PM

You can use google to search for other answers

Custom Search

More Threads

• Handling Function Keys
• Horizontal scollbars not appearing in CheckedListBox
• Help with migration from VB to C# (re: form controls)
• Application.EnableVisualStyles()
• ?? "Strange" GiveFeedback Event ??
• Dynamically add tab control
• Sound Corruption using SoundPlayer Control
• Using VBA (Access 2003) Themes in VB.Net
• Listview Alternate Row Color
• Point on PictureBox on Panel