How Add and remove list values in the Windows Listview controls.?

Hi,

I am using 2 Listview controls in the windows form.

I am moving values from one list to another list. (either single selection on multiple selection). This will be working fine.

When i'm trying to add the same value in list2, then

"it should not add the duplicate values". without throwing the error it should remove and add only new one.

How to do this....?
Ganesh STech Lead cum developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vishal TankariyaAsp.Net DeveloperCommented:
Hello Ganesh,

Same Item not allowed in listview. if you want to add same item in listview then either you need to change list value or change list text.
0
Ganesh STech Lead cum developerAuthor Commented:
it's allowing. that's why i raise a query.

anyhow i want to prevent to add if any duplicate over there...
0
it_saigeDeveloperCommented:
Like this:

Form1.cs -
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29084942
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            lvLeft.Items.AddRange((from i in Enumerable.Range(0, 25) select new ListViewItem($"Item{i}") { Tag = new { ID = i, Name = $"Item{i}", BirthDate = DateTime.Now.AddDays(-(9 * i + 3)) } }).ToArray());
        }

        private void OnClick(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                var btn = sender as Button;
                if (btn.Equals(btnAdd))
                {
                    lvRight.Items.AddRange((from left in lvLeft.SelectedItems.Cast<ListViewItem>()
                                            let matched = (from right in lvRight.Items.Cast<ListViewItem>()
                                                           where left.Tag.Equals(right.Tag)
                                                           select right).FirstOrDefault()
                                            where matched == null
                                            select left.Clone() as ListViewItem).ToArray());
                }
                else if (btn.Equals(btnRemove))
                {
                    foreach (var item in lvRight.SelectedItems.Cast<ListViewItem>())
                    {
                        lvRight.Items.Remove(item);
                    }
                }
            }
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29084942
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lvLeft = new System.Windows.Forms.ListView();
            this.btnRemove = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.lvRight = new System.Windows.Forms.ListView();
            this.SuspendLayout();
            // 
            // lvLeft
            // 
            this.lvLeft.Location = new System.Drawing.Point(13, 13);
            this.lvLeft.Name = "lvLeft";
            this.lvLeft.Size = new System.Drawing.Size(185, 316);
            this.lvLeft.TabIndex = 0;
            this.lvLeft.UseCompatibleStateImageBehavior = false;
            this.lvLeft.View = System.Windows.Forms.View.List;
            // 
            // btnRemove
            // 
            this.btnRemove.Location = new System.Drawing.Point(220, 174);
            this.btnRemove.Name = "btnRemove";
            this.btnRemove.Size = new System.Drawing.Size(75, 23);
            this.btnRemove.TabIndex = 5;
            this.btnRemove.Text = "<< Remove";
            this.btnRemove.UseVisualStyleBackColor = true;
            this.btnRemove.Click += new System.EventHandler(this.OnClick);
            // 
            // btnAdd
            // 
            this.btnAdd.Location = new System.Drawing.Point(220, 144);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(75, 23);
            this.btnAdd.TabIndex = 4;
            this.btnAdd.Text = "Add >>";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.OnClick);
            // 
            // lvRight
            // 
            this.lvRight.Location = new System.Drawing.Point(317, 12);
            this.lvRight.Name = "lvRight";
            this.lvRight.Size = new System.Drawing.Size(185, 316);
            this.lvRight.TabIndex = 6;
            this.lvRight.UseCompatibleStateImageBehavior = false;
            this.lvRight.View = System.Windows.Forms.View.List;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(514, 340);
            this.Controls.Add(this.lvRight);
            this.Controls.Add(this.btnRemove);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.lvLeft);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ListView lvLeft;
        private System.Windows.Forms.Button btnRemove;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.ListView lvRight;
    }
}

Open in new window


-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ganesh STech Lead cum developerAuthor Commented:
Thanks.closing points
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Listview

From novice to tech pro — start learning today.