SIGN UP MEMBER LOGIN:    
ARTICLE

How do I Check for Duplicate Items in a ListView?

Posted by Mike Gold Articles | How do I May 09, 2007
This "How To" Shows you how to Check for Duplicate Items in a ListView
Reader Level:

This How To was written in response to the following question in the C# Corner Forums about ListViews:

The user states: 

Question:  "I have code which I used to check duplication in a ListView,  but it doesn't seem to be working.  Here is my situation.  I have three columns in a ListView and I want to check to make sure that I don't duplicate a field when I add an item to the ListView. How do I do that?"
 

I tried this code, but it doesn't seem to work:

if  (listView1.Items.Contains(lvi) == false)
{
//Add the item to the ListView Control
listView1.Items.Add(lvi);
}
else
{
  //Warn user of duplicate entry...
 MessageBox.Show("Duplicate Item!");
}

Answer:

This will not work because you are probably not passing the exact same object, but an object that contains the same text.  Is this the situation?
For example this works fine with your code:

ListViewItem lvi = new ListViewItem("dog");
Add(lvi);
Add(lvi);

The second time you try to add the same object, you get the message box.
If you want to check the internal information in the row against your Add, you can provide a key in your item.  The key corresponds to the name of the item and can be used to compare items using the ContainsKey method:
 

if (!listView1.Items.ContainsKey(lvi.Name))
{
   //Add the item to the ListView Control
   listView1.Items.Add(lvi);
}
else
{
  //Warn user of duplicate entry...
 
MessageBox.Show("Duplicate Item!");
}


This will work for the following code with a unique name for your ListViewItem (the unique name being "item1"):

ListViewItem lvi1 = new ListViewItem("dog");
lvi1.Name =
"item1";
Add(lvi1);

Otherwise, if you don't provide a key,  you'll need to compare the list of items and check each subitem  within each item:
 

private bool IsInCollection(ListViewItem lvi)
 {
   foreach (ListViewItem item in listView1.Items)
   {
         bool subItemEqualFlag = true;
         for (int i = 0; i < item.SubItems.Count; i++)
            {
                 string sub1 = item.SubItems[i].Text;
                 string sub2 = lvi.SubItems[i].Text;
                 if (sub1 != sub2)
                  {
                     subItemEqualFlag =
false;
                  }
             }
            if (subItemEqualFlag)
                            return true;
     }

     return false;

}



 


 

Login to add your contents and source code to this article
share this article :
post comment
 

Sir, How to join tables from diffrent servers?

Posted by Prashant More Dec 09, 2007

Sir, How to delete rows which are duplicate?

Posted by Prashant More Dec 09, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor