Listview - Wont' Return Subitem Values

mrfrisky330

New member
Joined
Oct 16, 2021
Messages
1
Programming Experience
10+
Hello,

This one has me stumped.. using VS 2015.

Got a simple listview, 2 columns in "details" style, with labeledit set to true.
If column one has the value "hello" and a subitem 0 (column two) has value "bye", in code if I try this:

strTemp = this.LVitems.SelectedItems[0].Subitems[0].Text;

It ONLY returns "hello", NOT the subitem value - ??!!

Is this a bug in VS2015?
 
No. VS2015 has nothing to do with it. VS2015 is a compiler that only does things at compile time. Your problem is a runtime problem. I suspect that you've loaded that data into your listview incorrectly. We can't really tell unless you show us your code where you populate your list view.
 
Unless I'm missing something, what you describe is exactly what you should expect to see. If you have a ListView in Details view then what you see in the first column can be accessed via the Text property of an item AND via the Text property of the first subitem of that item. What you see in the second column is accessed via the Text property of the second subitem. If your ListView has two columns and one item with "hello" in the first column and "bye" in the second column then this code:
C#:
var itemText = myListView.Items[0].Text;
var firstSubItemText = myListView.Items[0].SubItems[0].Text;
var secondSubItemText = myListView.Items[0].SubItems[1].Text;

Console.WriteLine(itemText);
Console.WriteLine(firstSubItemText);
Console.WriteLine(secondSubItemText);
would produce this output:
hello
hello
bye
If you had bothered to read the relevant documentation then you'd have seen that for yourself:
The first subitem in the ListViewItem.ListViewSubItemCollection is always the item that owns the subitems. When performing operations on subitems in the collection, be sure to reference index position 1 instead of 0 to make changes to the first subitem.
 
Back
Top Bottom