how do i move all of the items from my listview (listview2) to my main form listview (lv_Collection)?
public Main_Form mainref;
private void btnOK_Click(object sender, EventArgs e)
{
ListViewItem itemClone = new ListViewItem();
ListView.ListViewItemCollection col1 = listView2.Items;
foreach (ListViewItem item in col1)
{
itemClone = item.Clone() as ListViewItem;
listView2.Items.Remove(item);
mainref.lv_Collection.Items.Add(itemClone);
//mainref.Refresh();
}
this.Close();
}
I got an error on this code " mainref.lv_Collection.Items.Add(itemClone);
"
My main form code
Collection_Forms.Collection_Accredited_Select frm = new Collection_Forms.Collection_Accredited_Select();
frm.ShowDialog();
frm.mainref = this;
No need to clone them if you want to move them all:
private void btnOK_Click(object sender, EventArgs e)
{
int count = listView1.Items.Count;
for (int i = 0; i < count; i++)
{
ListViewItem lvi = listView1.Items[listView1.Items.Count - 1];
listView1.Items.Remove(lvi);
listView2.Items.Add(lvi);
}
}
If you want to move only some items best collect them in a List<ListViewItem>
and then loop over the list.
I assume that you really want to move the complete items, not just their subitems?
Also note that you are not allowed to modify the collection you are iterating over in a foreach
loop..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.