Reputation: 1
Within my winform(C#) I have listView with two columns, which is used to display the results from searching an array, which is the basic details of an array entry(All data within the array is type string). I am able to add and remove items to the listview, but I would like to be able to gain the value of the row that has been clicked within the listview.
I am planning to use this to update the display full entry info, but I cannot gain the value of the selected item. I was reading elsewhere and saw people mentioning the .SelectedIndex property, but this property is not available to me when I am trying to code.
I could use two listboxes instead of a listview, but the listview is much neater. Also, I am a sofware design (high school) student, and have been learning C# for one and a half years. I am competent at programming, but I start to get lost when things start to get very complex.
Can anybody help?
Upvotes: 0
Views: 4409
Reputation: 1
I have fixed the problem. After talking to a friend in my software class, he told me to use listView1.FocusedItem.Index
, which works perfectly for my needs, as it gains the index of the selected item. Also, thank you to everyone for their input in trying to help me.
Upvotes: 0
Reputation: 6637
You could trap the SelectedIndexChanged
event and in that you could do
ListView.SelectedListViewItemCollection listItems=
this.myListView.SelectedItems;
foreach ( ListViewItem item in listItems)
{
MessageBox.Show(item.SubItems[0].Text);
MessageBox.Show(item.SubItems[1].Text);
}
Upvotes: 0
Reputation: 4114
you should use the SelectedItem property and Tag property
var item = (MyClass) listView1.SelectedItems[0].Tag;
tag property allow you to set any type for example MyClass when you populate the ListView set the Tag property.
Upvotes: 0
Reputation: 33272
The ListView.SelectedItems
property works fine if the ListView is not in VirtualData mode.
Upvotes: 2