/ Published in: C#
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).
See also:
[http://www.codeproject.com/KB/list/dragdroplistviewreorder1.aspx](http://www.codeproject.com/KB/list/dragdroplistviewreorder1.aspx).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System.Drawing; using System.Windows.Forms; namespace System.Windows.Forms // May need to set to something else { /// <summary> /// A ListView with DragDrop reordering. /// <see cref="http://support.microsoft.com/kb/822483/en-us"/> /// </summary> public class ListViewWithReordering : ListView { protected override void OnItemDrag(ItemDragEventArgs e) { base.OnItemDrag(e); //Begins a drag-and-drop operation in the ListView control. this.DoDragDrop(this.SelectedItems, DragDropEffects.Move); } protected override void OnDragEnter(DragEventArgs drgevent) { base.OnDragEnter(drgevent); int len = drgevent.Data.GetFormats().Length - 1; int i; for (i = 0; i <= len; i++) { if (drgevent.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection")) { //The data from the drag source is moved to the target. drgevent.Effect = DragDropEffects.Move; } } } protected override void OnDragDrop(DragEventArgs drgevent) { base.OnDragDrop(drgevent); //Return if the items are not selected in the ListView control. if (this.SelectedItems.Count == 0) { return; } //Returns the location of the mouse pointer in the ListView control. //Obtain the item that is located at the specified location of the mouse pointer. ListViewItem dragToItem = this.GetItemAt(cp.X, cp.Y); if (dragToItem == null) { return; } //Obtain the index of the item at the mouse pointer. int dragIndex = dragToItem.Index; for (int i = 0; i <= this.SelectedItems.Count - 1; i++) { sel[i] = this.SelectedItems[i]; } for (int i = 0; i < sel.GetLength(0); i++) { //Obtain the ListViewItem to be dragged to the target location. ListViewItem dragItem = sel[i]; int itemIndex = dragIndex; if (itemIndex == dragItem.Index) { return; } if (dragItem.Index < itemIndex) itemIndex++; else itemIndex = dragIndex + i; //Insert the item at the mouse pointer. ListViewItem insertItem = (ListViewItem)dragItem.Clone(); this.Items.Insert(itemIndex, insertItem); //Removes the item from the initial location while //the item is moved to the new location. this.Items.Remove(dragItem); } } } }
URL: http://support.microsoft.com/kb/822483/en-us