Prevent double entries in ListView using C#?

前端 未结 6 1844
日久生厌
日久生厌 2021-01-16 09:59

How can we access the items added to a ListView?

The thing I have to do is: add an item to the list view. I want to check if the item to add to the listview is alrea

6条回答
  •  野的像风
    2021-01-16 10:31

    The following will help to locate a ListViewItem within the ListView control once you've added it:

    string key = ;
    if (!theListViewControl.Items.ContainsKey(key))
    {
        item = theListViewControl.Items.Add(key, "initial text", -1);
    
    }
    
    // now we get the list item based on the key, since we already 
    // added it if it does not exist
    item = theListViewControl.Items[key];
    ...
    

    Note The key used to add the item to the ListView items collection can be any unique value that can identify the ListViewItem within the collection of items. For example, it could be a hashcode value or some property on an object attached to the ListViewItem.

提交回复
热议问题