How to check an item already entered in a listview if i am entering data from a combo box from both tables for an id as value member...I mean there is both software id and asset id that can be added mixed in the listview based on the group....

Dani AI

Generated

described a common issue: software IDs and asset IDs can overlap, so a plain ID check may produce false duplicates when items from different groups are mixed. 's hint that items should be keyed is useful; here are practical, robust ways to implement that and handle duplicates cleanly.

Use a stable composite key (group + separator + id) and keep a fast lookup (HashSet or Dictionary) alongside the ListView. Store the key in the ListViewItem.Tag so the ListView always carries the source data. Example (C#):

// form-level
HashSet<string> addedKeys = new HashSet<string>();

void AddItem(string group, string id, string display)
{
    string key = group + ":" + id; // "Software:42" vs "Asset:42"
    if (!addedKeys.Add(key))
    {
        // duplicate found: select existing item
        var existing = listView.Items
            .Cast<ListViewItem>()
            .FirstOrDefault(i => (string)i.Tag == key);
        if (existing != null) { existing.Selected = true; existing.EnsureVisible(); }
        return;
    }

    var item = new ListViewItem(display) { Tag = key };
    listView.Items.Add(item);
}

If you need O(1) access to the ListViewItem for updates, use a Dictionary<string, ListViewItem> and TryGetValue when adding/removing. When removing or editing items, update the HashSet/Dictionary accordingly. For very large lists, avoid repeated enumeration of Items and prefer the Dictionary approach.

Practical notes: always normalize group and id strings (trim, consistent casing if strings), decide desired duplicate behaviour upfront (ignore, select, increment quantity, or overwrite), and perform any UI changes on the UI thread. These steps avoid accidental collisions between "software" and "asset" IDs and give predictable, maintainable duplicate handling.

Recommended Answers

All 3 Replies

When you add the items to the listview set the Name property of the item to equal the Text. This way you can use the ContainsKey method to check if it exists.

How to check an item already entered in a listview if i am entering data from a combo box from two tables for an id as value member based on group...I mean there is both software id and asset id that can be added in the listview based on the group software and hardware....If software software item with software id else asset item with asset id will be added to listview....How to check duplicate items while adding to listview if so....

When you add the items to the listview set the Name property of the item to equal the Text. This way you can use the ContainsKey method to check if it exists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.