CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Dec 2010
    Posts
    8

    Copying listview row without duplication

    Hi,

    Trying to copy a ListView row to another listView control and to prevent having duplicates I'm checking if the item already exists first.
    If the row doesn't exist then copy it if not don't do anything.
    My code doesn't seem to work and thinks the row does't exist all the time.
    could someone tell me where I am going wrong?
    Thanks and advance.

    private void CopyChecked ( ListView sourceListView, ListView destListView )
    {
    foreach ( ListViewItem item in sourceListView.Items )
    if (item.Checked)
    {
    if (CheckIfListViewItemExistsInListView(item,destListView)==false)
    {
    destListView.Items.Add(item.Clone() as ListViewItem);
    }
    }
    }



    ------------------------
    private bool CheckIfListViewItemExistsInListView(ListViewItem itemToAdd, ListView destListView)
    {
    bool exists = false;
    foreach (ListViewItem item in destListView.Items)
    {
    if(item == itemToAdd)
    {
    exists=true;
    break; // Break the loop if the item found.
    }
    }
    return exists;

    }
    Last edited by walidm38; January 12th, 2011 at 08:50 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured