Link to home
Start Free TrialLog in
Avatar of kranthi4uonly
kranthi4uonly

asked on

listview duplicates

how t o check for duplicates in a listview while adding it from a listbox
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

you can check by using ListView.public ListViewItem FindItemWithText() method.
It can search for a text in the first column, and there are other versions to check in other columns. I suggest to use the first one.
Sorry, I meant:
you can check by using ListView.FindItemWithText() method.
Avatar of longtruong
Hi,

You should use ListView.FindItemWithText() as suggested by jaime_olivares.  However, I am not sure if it is enough so I would like to give some additional information here.  If the item text is unique then this method is OK.  However, if the list has several items with the same text, this method always returns the first matched item.  To solve this situation, I suggest that when adding an item to the list, please do not forget associating a unique data to Tag attribute of each item and the Tag's value will be used as a key to search for a duplicate later.

Attached sample code is provided.

Hope it helps.

Long
public bool CheckDuplicate(int id)
{
    for (int i = 0; i < listView1.Items.Count; i++)
    {
        ListViewItem lvi = listView1.Items[i];
        if (lvi.Tag == (int)id)
        {
            return true;
        }
    }
    return false;
}
 
private void button1_Click(object sender, EventArgs e)
{
    //Check for duplicate
    if (CheckDuplicate(<your id here>) == false)
    {
        //Add new item to the list
        ListViewItem lvi = new ListViewItem();
        lvi.Text = "your item text here";
        lvi.Tag = "your id here";
        listView1.Items.Add(lvi);
    }
}

Open in new window

Avatar of kranthi4uonly
kranthi4uonly

ASKER

hi longtruong:
i dont have duplicates in listview
iam adding it from a listbox into list view
iam using the following code but why it is not working i cant understand


private void button1_Click(object sender, EventArgs e)
        {
           
            ListViewItem item = new ListViewItem(listBox1.SelectedItem.ToString());
            for (int i = 0; i < listView1.Items.Count; i++)
            {

                ListViewItem s =new ListViewItem(item.Text);
                if (listView1.Items[i].Text.Contains(s.Text))
                {
                    break ;
                }
                //else
                {

                    listView1.Items.AddRange(new ListViewItem[] { item });
                    break;


                }
what about my suggestion?
yeah iam using ur code as like this but its saying object reference not set to an instance of object this works fien if their are duplicates
if not it gives exception as mentioned above
am i doing right
private void button1_Click(object sender, EventArgs e)
        {
           
            ListViewItem item = new ListViewItem(listBox1.SelectedItem.ToString());
            for (int i = 0; i < listView1.Items.Count; i++)
            {

                ListViewItem s =new ListViewItem(item.Text);
                ListViewItem j = listView1.FindItemWithText(s.Text);
                if (j.Text==s.Text && j!=null)
                {
                   
                    break ;
                }
                //else
                {
                    listView1.Items.AddRange(new ListViewItem[] { item });
                   
                    break;


                }
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
excelent