ListView inserted item's index
I have a ListView and sorting is enabled, once I 开发者_StackOverflowinsert an item, how do I get that item's index?
I can use the ListViewItem returned by the Add method of ListView, but is its index before sort or after? And is it updated if it is sorted again? Like if I store the ListViewItem and the ListView is then sorted, will the ListViewItem I stored earlier, its Index property would be updated too?
Depends on how you add:
ListViewItem item = ...;
listView.Items.Add(item);
int index = item.Index;
or
ListViewItem item = listView.Items.Add("ItemName");
int index = item.Index;
To answer your questions that I missed before: yes, the indexes should be updated.
Assuming you are adding a ListViewItem
:
listView.Items.Add(myItem);
int index = listView.Items.IndexOf(myItem);
Try using listView.Items.IndexOfKey(name_of_your_item_goes_here)
or listView.Items.IndexOf(your_new_item)
if you care to create ListViewItems before adding them to list.
精彩评论