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?
 
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
 
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
 
Ganesh STech Lead cum developerAuthor Commented:
Thanks.closing points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.