Prevent double entries in ListView using C#?

前端 未结 6 1841
日久生厌
日久生厌 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:15

    You could do something like this:

     ListViewItem itemToAdd;
     bool exists = false;
     foreach (ListViewItem item in yourListView.Items) 
     {
        if(item == itemToAdd)
            exists=true;
     }
    
     if(!exists)
         yourListView.Items.Add(itemToAdd);
    
    0 讨论(0)
  • 2021-01-16 10:23

    Just add your items and make sure you assign a name. Then just use the ContainsKey method of the Items collection to determine if it's there, like this.

    for (int i = 0; i < 20; i++)
    {
        ListViewItem item = new ListViewItem("Item" + i.ToString("00"));
        item.Name = "Item"+ i.ToString("00");
        listView1.Items.Add(item);
    }
    MessageBox.Show(listView1.Items.ContainsKey("Item00").ToString()); // True
    MessageBox.Show(listView1.Items.ContainsKey("Item20").ToString()); // False
    
    0 讨论(0)
  • 2021-01-16 10:31

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

    string key = <some generated value that defines the key per item>;
    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.

    0 讨论(0)
  • 2021-01-16 10:32

    The ListView class provides a few different methods to determine if an item exists:

    • Using Contains on the Items collection
    • Using one of the FindItemWithText methods

    They can be used in the following manner:

    // assuming you had a pre-existing item
    ListViewItem item = ListView1.FindItemWithText("test");
    if (!ListView1.Items.Contains(item))
    {
        // doesn't exist, add it
    }
    
    // or you could find it by the item's text value
    ListViewItem item = ListView1.FindItemWithText("test");
    if (item != null)
    {
        // it exists
    }
    else
    {
        // doesn't exist
    }
    
    // you can also use the overloaded method to match sub items
    ListViewItem item = ListView1.FindItemWithText("world", true, 0);
    
    0 讨论(0)
  • 2021-01-16 10:38

    A small correction in Robban's answer

     ListViewItem itemToAdd;
     bool exists = false;
     foreach (ListViewItem item in yourListView.Items)
     {
        if(item == itemToAdd)
        {
           exists=true; 
           break; // Break the loop if the item found.
        }
     }
     if(!exists)
     {
        yourListView.Items.Add(itemToAdd);
     }
     else
     {
        MessageBox.Show("This item already exists");
     }
    
    0 讨论(0)
  • 2021-01-16 10:40

    In case of multicolumn ListView, you can use following code to prevent duplicate entry according to any column:

    Let us suppose there is a class Judge like this

    public class Judge
        {
            public string judgename;
            public bool judgement;
            public string sequence;
            public bool author;
            public int id;
    
        }
    

    And i want to add unique object of this class in a ListView. In this class id is unique field, so I can check unique record in ListView with the help of this field.

    Judge judge = new Judge
                    {
                        judgename = comboName.Text,
                        judgement = checkjudgement.Checked,
                        sequence = txtsequence.Text,
                        author = checkauthor.Checked,
                        id = Convert.ToInt32(comboName.SelectedValue)
                    };
    
                ListViewItem lvi = new ListViewItem(judge.judgename);
                lvi.SubItems.Add(judge.judgement ? "Yes" : "No");
                lvi.SubItems.Add(string.IsNullOrEmpty(judge.sequence) ? "" : txtsequence.Text);
                lvi.SubItems.Add(judge.author ? "Yes" : "No");
                lvi.SubItems.Add((judge.id).ToString());
                if (listView1.Items.Count != 0)
                {
                    ListViewItem item = listView1.FindItemWithText(comboName.SelectedValue.ToString(), true, 0);
                    if (item != null)
                    {
                        // it exists
                    }
                    else
                    {
                        // doesn't exist
                    }
    
                }
    
    0 讨论(0)
提交回复
热议问题