开发者

ListView inserted item's index

开发者 https://www.devze.com 2023-01-08 03:45 出处:网络
I have a ListView and sorting is enabled, once I 开发者_StackOverflowinsert an item, how do I get that 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消