kranthi4uonly
asked on
listview duplicates
how t o check for duplicates in a listview while adding it from a listbox
Sorry, I meant:
you can check by using ListView.FindItemWithText( ) method.
you can check by using ListView.FindItemWithText(
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
You should use ListView.FindItemWithText(
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);
}
}
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.Sele ctedItem.T oString()) ;
for (int i = 0; i < listView1.Items.Count; i++)
{
ListViewItem s =new ListViewItem(item.Text);
if (listView1.Items[i].Text.C ontains(s. Text))
{
break ;
}
//else
{
listView1.Items.AddRange(n ew ListViewItem[] { item });
break;
}
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.Sele
for (int i = 0; i < listView1.Items.Count; i++)
{
ListViewItem s =new ListViewItem(item.Text);
if (listView1.Items[i].Text.C
{
break ;
}
//else
{
listView1.Items.AddRange(n
break;
}
what about my suggestion?
ASKER
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.Sele ctedItem.T oString()) ;
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(n ew ListViewItem[] { item });
break;
}
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.Sele
for (int i = 0; i < listView1.Items.Count; i++)
{
ListViewItem s =new ListViewItem(item.Text);
ListViewItem j = listView1.FindItemWithText
if (j.Text==s.Text && j!=null)
{
break ;
}
//else
{
listView1.Items.AddRange(n
break;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
excelent
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.