1
A
回答
0
两个ASP.NET和WinForms ListView
有Items
属性,它允许添加或删除的项目。
0
我写了一个应该有效的小样本。
ListViewItem[] copyOfItemsInListView1 = new ListViewItem[listView1.Items.Count];
ListViewItem[] copyOfItemsInListView2 = new ListViewItem[listView2.Items.Count];
listView1.Items.CopyTo(copyOfItemsInListView1, 0);
listView2.Items.CopyTo(copyOfItemsInListView2, 0);
listView1.Items.Clear();
listView2.Items.Clear();
for (int i = 0; i < copyOfItemsInListView2.Length; i++)
{
listView1.Items.Add(copyOfItemsInListView2[i]);
}
for (int i = 0; i < copyOfItemsInListView1.Length; i++)
{
listView2.Items.Add(copyOfItemsInListView1[i]);
}
0
克隆他们:经穆拉特ref'd的KnowDotNet文章
// move selected item up
int selectedIndex = mListView.SelectedIndices[0];
if (selectedIndex > 0)
{
ListViewItem item1 = (ListViewItem)mListView.Items[selectedIndex - 1].Clone();
ListViewItem item2 = (ListViewItem)mListView.Items[selectedIndex].Clone();
mListView.Items[selectedIndex - 1] = item2;
mListView.Items[selectedIndex] = item1;
mListView.SelectedIndices.Remove(selectedIndex);
mListView.SelectedIndices.Add(selectedIndex - 1);
}
1
大厦,这里是我的扩展方法是一个比较灵活的(它作用于任何项目,而不仅仅是cursel) ,并修复bug(BeginUpdate/Endupdate更少闪烁,EnsureVisible和边界检查)。 并不需要是一个扩展方法,但我喜欢他们:)
namespace YourApp
{
public static class MyExtensions
{
// Based upon http://www.knowdotnet.com/articles/listviewmoveitem.html
public static void MoveSelectedItem(this System.Windows.Forms.ListView lv, int idx, bool moveUp)
{
// Gotta have >1 item in order to move
if(lv.Items.Count > 1)
{
int offset = 0;
if (idx >= 0)
{
if (moveUp)
{
// ignore moveup of row(0)
offset = -1;
}
else
{
// ignore movedown of last item
if (idx < (lv.Items.Count - 1))
offset = 1;
}
}
if (offset != 0)
{
lv.BeginUpdate();
int selitem = idx + offset;
for (int i = 0; i < lv.Items[idx].SubItems.Count; i++)
{
string cache = lv.Items[selitem].SubItems[i].Text;
lv.Items[selitem].SubItems[i].Text = lv.Items[idx].SubItems[i].Text;
lv.Items[idx].SubItems[i].Text = cache;
}
lv.Focus();
lv.Items[selitem].Selected = true;
lv.EnsureVisible(selitem);
lv.EndUpdate();
}
}
}
}
}
1
如果使用自定义的ListViewItem,或对象无法克隆的对象,或股票字符串:
enum Direction { UP = -1, DOWN = +1};
void ListViewMove(ListView lv, Direction direction)
{
if (lv.SelectedItems.Count > 0)
{
int selIdx = lv.SelectedItems[0].Index;
ListViewItem tmp = lv.Items[selIdx] ;
if (((selIdx != 0) && direction == Direction.UP) ||
((selIdx!=lv.Items.Count-1) && (direction == Direction.DOWN)))
{
lv.Items.RemoveAt(selIdx);
tmp = lv.Items.Insert(selIdx + (int)direction, tmp);
tmp.Selected = true;
}
}
lv.Focus();
}
相关问题
- 1. 交换两个整数值的最佳方法是什么?
- 2. 在C#中检查ListView以检查项目的最佳方法是什么?
- 3. 什么是将C#项目文件转换为Objective C类的最佳方式
- 4. 组织C#项目的最佳方式是什么?
- 5. 同步两个java项目的最佳方法是什么?
- 6. 什么是在项目中组织CSS/JavaScript的最佳方式
- 7. 在RoR中重用项目的最佳方式是什么?
- 8. 在C#中切换表单的最佳方式是什么?
- 9. 在GWT中比较两个JavaScriptObjects的最佳方式是什么?
- 10. 合并两个Django项目用户表的最佳方式是什么?
- 11. 在github上存储Symfony项目的最佳方式是什么?
- 12. 什么是在Linux上编译jsp项目的最佳方式?
- 13. 什么是在线工作MVC项目的最佳方式?
- 14. 在aws上运行django项目的最佳方式是什么?
- 15. 在c#webform项目中使用SignalR的最佳方式是什么?
- 16. 什么是比较两个项目列表的最快方式?
- 17. CMake在一个中型项目中的最佳方式是什么?
- 18. 在python中做一个真实的GUI项目的最佳方式是什么?
- 19. 在c#中比较两个pdf文件的最佳方式是什么?
- 20. 在C#中返回两个列表的最佳方式是什么?
- 21. 在红宝石中组织32个项目的最佳方式是什么?
- 22. 在WordPress中格式化C#的最佳方式是什么?
- 23. 最佳实践 - 什么是观看目录的最佳方式
- 24. python中交互式调试的最佳方式是什么?
- 25. 什么是分析项目风险的最佳方式?
- 26. 什么是配置Django项目的最佳方式
- 27. 什么是项目的最佳版本控制方式
- 28. 将IntelliJ项目迁移到Maven的最佳方式是什么?
- 29. 什么是为Android项目添加背景的最佳方式?
- 30. 构建项目的最佳方式是什么?
今非昔比OP要求:这将它们从一个LV控制复制到另一个 – dlchambers 2012-10-07 12:41:19
正确。我再次阅读这个问题,看到我误解了这个问题:) – Javier 2012-10-08 14:55:10