tags:

views:

204

answers:

3

Hi friends

I have Two ListViews... I have ListView(ListView1) with items, and another one(ListView2) is empty. i need to copy the checked items from ListView1 to ListView2 at the same time i have to remove the checked item in ListView1 using C#...

I am using windows form...

Thanks in advance...

+1  A: 

Something like this? (Not tested!!)

for (int i = ListView1.Items.Count - 1; i >= 0; i--)
{
  if (ListView1.Items[i].Checked)
  {
    ListView2.Items.Add(ListView1.Items[i]);
    ListView1.Items.Remove(ListView.Items[i]);
    // OR
    // ListView1.Items.RemoveAt(i);
  }
}

EDIT:
This might be better.

CheckedListViewCollection checked = ListView1.CheckedItems
foreach (itm in checked)
{
  ListView2.Items.Add(itm);
  ListView1.Items.Remove(itm);
}
Sani Huttunen
The use of CheckedListViewCollection is nice.
RRUZ
A: 

Hi

//Copy ListView

for (int i = ListView1.Items.Count - 1; i >= 0; i--)
{
  if (ListView1.Items[i].Checked)
  {
    ListView2.Add(ListView1.Items[i]);
  }
}


//Delete checked 

ListView1.CheckedIndexCollection checkedItemsList = listView1.CheckedIndices;     
while (checkedItemsList.Count > 0)
{
   listView1.Items.RemoveAt(checkedItemsList[0]);
}

Bye

RRUZ
You've got an error in your for loop. Should read i--. Not i++.
Sani Huttunen
Apparently so did I!!! :D
Sani Huttunen
Thanks, CKret.
RRUZ
A: 
while (listView1.CheckedItems.Count > 0)
{
    ListViewItem item = listView1.CheckedItems[0];
    listView1.Items.Remove(item);
    listView2.Items.Add(item);
}

Tested.

Note the order of the item removal and adding. The framework throws up an exception if you try to add a ListViewItem to more than one list at the same time.

Jon Seigel