开发者

.NET Listview Copy Items

Is there a way to copy the entire contents of a listview from one control to another, without manually setting up the second one and开发者_如何学Go iterating through each item? I'm thinking something like:

ListView myNewListView = new ListView();
lvwExistingView.CopyTo(myNewListView);

Or even:

ListView myNewListView = new ListView();
lvwExistingView.Items.CopyTo(myNewListView.Items, 1); // This doesn't work because it expects an array


A ListViewItem can't exist on more than one list view at one time. They need to be recreated.

But you can use LINQ to make this quick.

myNewListView.Items.AddRange(
    lvwExistingView.Items.Cast<ListViewItem>().Select(i => 
        i.Clone()).Cast<ListViewItem>().ToArray());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜