ListView with re-ordering through drag and drop


/ Published in: C#
Save to your folder(s)

Microsoft has acknowledged (see URL) that the lack of this functionality is an error. The extended control code below adds reordering to a ListView. It works with multiple items as well as single items.

See also:
[http://www.codeproject.com/KB/list/dragdroplistviewreorder1.aspx](http://www.codeproject.com/KB/list/dragdroplistviewreorder1.aspx).


Copy this code and paste it in your HTML
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3.  
  4. namespace System.Windows.Forms // May need to set to something else
  5. {
  6. /// <summary>
  7. /// A ListView with DragDrop reordering.
  8. /// <see cref="http://support.microsoft.com/kb/822483/en-us"/>
  9. /// </summary>
  10. public class ListViewWithReordering : ListView
  11. {
  12. protected override void OnItemDrag(ItemDragEventArgs e)
  13. {
  14. base.OnItemDrag(e);
  15. //Begins a drag-and-drop operation in the ListView control.
  16. this.DoDragDrop(this.SelectedItems, DragDropEffects.Move);
  17. }
  18.  
  19. protected override void OnDragEnter(DragEventArgs drgevent)
  20. {
  21. base.OnDragEnter(drgevent);
  22. int len = drgevent.Data.GetFormats().Length - 1;
  23. int i;
  24. for (i = 0; i <= len; i++)
  25. {
  26. if (drgevent.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
  27. {
  28. //The data from the drag source is moved to the target.
  29. drgevent.Effect = DragDropEffects.Move;
  30. }
  31. }
  32.  
  33. }
  34.  
  35. protected override void OnDragDrop(DragEventArgs drgevent)
  36. {
  37. base.OnDragDrop(drgevent);
  38. //Return if the items are not selected in the ListView control.
  39. if (this.SelectedItems.Count == 0)
  40. {
  41. return;
  42. }
  43. //Returns the location of the mouse pointer in the ListView control.
  44. Point cp = this.PointToClient(new Point(drgevent.X, drgevent.Y));
  45. //Obtain the item that is located at the specified location of the mouse pointer.
  46. ListViewItem dragToItem = this.GetItemAt(cp.X, cp.Y);
  47. if (dragToItem == null)
  48. {
  49. return;
  50. }
  51. //Obtain the index of the item at the mouse pointer.
  52. int dragIndex = dragToItem.Index;
  53. ListViewItem[] sel = new ListViewItem[this.SelectedItems.Count];
  54. for (int i = 0; i <= this.SelectedItems.Count - 1; i++)
  55. {
  56. sel[i] = this.SelectedItems[i];
  57. }
  58. for (int i = 0; i < sel.GetLength(0); i++)
  59. {
  60. //Obtain the ListViewItem to be dragged to the target location.
  61. ListViewItem dragItem = sel[i];
  62. int itemIndex = dragIndex;
  63. if (itemIndex == dragItem.Index)
  64. {
  65. return;
  66. }
  67. if (dragItem.Index < itemIndex)
  68. itemIndex++;
  69. else
  70. itemIndex = dragIndex + i;
  71. //Insert the item at the mouse pointer.
  72. ListViewItem insertItem = (ListViewItem)dragItem.Clone();
  73. this.Items.Insert(itemIndex, insertItem);
  74. //Removes the item from the initial location while
  75. //the item is moved to the new location.
  76. this.Items.Remove(dragItem);
  77. }
  78. }
  79. }
  80. }

URL: http://support.microsoft.com/kb/822483/en-us

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.