Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have

private void lvSource_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}


private void lvSource_ItemDrag(object sender, ItemDragEventArgs e)
{
lvSource.DoDragDrop(lvSource.SelectedItems, DragDropEffects.Move);
}

private void lvSource_DragDrop(object sender, DragEventArgs e)
{
if(lvSource.SelectedItems.Count == 0) { return; }

Point Pt = lvSource.PointToClient(new Point(e.X, e.Y));
ListViewItem ItemDrag = lvSource.GetItemAt(Pt.X, Pt.Y);

if(ItemDrag==null) { return; }
int ItemDragIndex = ItemDrag.Index;

ListViewItem[] sel = new ListViewItem[lvSource.SelectedItems.Count];

for (int i = 0; i < lvSource.SelectedItems.Count; i++)
{
sel[i] = lvSource.SelectedItems[i];
}

for (int i = 0; i < sel.GetLength(0); i++)
{
ListViewItem Item = sel[i];
int ItemIndex = ItemDragIndex;

if(ItemIndex == Item.Index) { return; }

if(Item.Index < ItemIndex)
{
ItemIndex++;
}
else
{
ItemIndex = ItemDragIndex + 1;
}

ListViewItem InsertItem = (ListViewItem)Item.Clone();
lvSource.Items.Insert(ItemIndex, InsertItem);
lvSource.Items.Remove(Item);
}

}

private void lvDestination_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}


private void lvDestination_DragDrop(object sender, DragEventArgs e)
{
if (lvDestination.SelectedItems.Count == 0) { return; }

Point Pt = lvDestination.PointToClient(new Point(e.X, e.Y));
ListViewItem ItemDrag = lvDestination.GetItemAt(Pt.X, Pt.Y);

if (ItemDrag == null) { return; }
int ItemDragIndex = ItemDrag.Index;

ListViewItem[] sel = new ListViewItem[lvDestination.SelectedItems.Count];

for (int i = 0; i < lvDestination.SelectedItems.Count; i++)
{
sel[i] = lvDestination.SelectedItems[i];
}

for (int i = 0; i < sel.GetLength(0); i++)
{
ListViewItem Item = sel[i];
int ItemIndex = ItemDragIndex;

if (ItemIndex == Item.Index) { return; }

if (Item.Index < ItemIndex)
{
ItemIndex++;
}
else
{
ItemIndex = ItemDragIndex + 1;
}

ListViewItem InsertItem = (ListViewItem)Item.Clone();
lvDestination.Items.Insert(ItemIndex, InsertItem);
lvDestination.Items.Remove(Item);
}
}

private void lvDestination_ItemDrag(object sender, ItemDragEventArgs e)
{
lvDestination.DoDragDrop(lvDestination.SelectedItems, DragDropEffects.Move);
}

private void SourceDestination_DrapDrop(object sender, DragEventArgs e)
{
//..... Code Here
}

I want to drag and drop items between two listview. Who can help me?
Posted
Comments
BillWoodruff 20-Jan-15 6:53am    
Is there a question ?

1 solution

What about consulting some tutorial before pasting code (without formatting)?
Have a look here: Drag and Drop between list boxes - Beginner's Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900