apony
apony

Reputation: 75

How to add new item in front of the listview / windows forms

I created a ListView and in my program I need to add items while my program is working, and every item must be on the top of the list until the new item is added. How can I do that? I tried with ;

ListView.Item.Add(0, item);

but its not working/

The second question is; is it possible for one item to be always on the top of this list and can I upgreade its value somehow?

Upvotes: 0

Views: 1341

Answers (1)

coolmine
coolmine

Reputation: 4463

If you always want to insert an item at the top you can use Items.Insert() method to do that

ListView.Items.Insert(0, item);

For editing an items value in the ListView simply change it's value as follows

ListView.Items[listViewItemIndex].Text = "New name";

If you want to keep an item always on top simple have an if statement that inserts the new items in position 1 instead of 0. Make sure that the number of items in the ListView is at least one or you will get an exception.

Upvotes: 3

Related Questions