How to copy Checked items from first listview to another listview?
i got problem with that, i made at the first listview Check boxes to every item
and i want when i will click a button all the checked items will be copy (not move) to the other listView.

it will be great if you would help me with that.
thanks

public Form1()
        {
            InitializeComponent();

            listView1.Columns.Add("Checking", 50, HorizontalAlignment.Center);
            listView1.Columns.Add("Item name", -2, HorizontalAlignment.Center);
            listView1.View = View.Details;
            listView1.CheckBoxes = true;

            listView2.Columns.Add("Item name", 100, HorizontalAlignment.Center);
            listView2.View = View.Details;

            //add some items to listView1:
            for (int i = 0; i < 5; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Add("Item " + i + 1);
                listView1.Items.Add(lvi);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Checked)
                {
                    listView2.Items.Add(listView1.Items[i].SubItems[1].Text);
                }
            }
        }

This code is better, it checks if the item exists in listView2. If it does not exists, it adds the item to listView2, if it exists, it does NOT add (it skips that one):

//code in the button click event:
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Checked)
                {
                    string _item = listView1.Items[i].SubItems[1].Text;
                    
                    //checking if this item exists in listView2:
                    //if not, its added to listView2
                    bool bInsert = true;
                    for (int j = 0; j < listView2.Items.Count; j++)
                    {
                        if (listView2.Items[j].Text == _item)
                        {
                            bInsert = false;
                            break;
                        }
                    }
                    if (bInsert)
                        listView2.Items.Add(_item);
                }
            }

actually the items always changed because i take them from XML file that i made, so i tried to delete the items that you made but it doesn't work.. i tried to do something but it don't work can you fix it for me?
and also my listView1 is producatsLV and my listView2 is selectedPDLV

public Form1()
        {
            InitializeComponent();
 
            producatsLV.Columns.Add("Checking", 50, HorizontalAlignment.Center);
            producatsLV.Columns.Add("Item name", -2, HorizontalAlignment.Center);
            producatsLV.View = View.Details;
            producatsLV.CheckBoxes = true;
 
            selectedPDLV.Columns.Add("Item name", 100, HorizontalAlignment.Center);
            selectedPDLV.View = View.Details;

            }
        }
for (int i = 0; i < producatsLV.Items.Count; i++)
            {
                if (producatsLV.Items[i].Checked)
                {
                    string _item = producatsLV.Items[i].SubItems[1].Text;

                    //checking if this item exists in listView2:
                    //if not, its added to listView2
                    bool bInsert = true;
                    for (int j = 0; j < selectedPDLV.Items.Count; j++)
                    {
                        if (selectedPDLV.Items[j].Text == _item)
                        {
                            bInsert = false;
                            break;
                        }
                    }
                    if (bInsert)
                        selectedPDLV.Items.Add(_item);
                }
            }

ok never mind something else was broken hh it's work :) thank you very much!! it was very helpfull

Actually i have one more question.
i want that if i will remove the check from item it will remove it from the listView2

also never mind, hh i tried and it worked. thank you!!! solved

Can u please give me the code .. how u loaded the treeview or listview from database... please i'm really struggling to do that,.....

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.