ListView (Details) Drag Drop

0
0

Am I missing something here? I’m trying to “drag” from ListView2 and “drop” to ListView1

 

private void Window1_Load(object sender, EventArgs e)
{
listView1.AllowDrop = true;
listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
}

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

private void listView1_DragDrop(object sender, DragEventArgs e)
{
listView1.Items.Add(e.Data.ToString());
}

  • You must to post comments
1
0

To drag from listView2:

listView2.AllowDrag = true;

listView2.DragStart += listView2_DragStart;

private void listView2_DragStart(object sender, EventArgs e)
{
var listView = (ListView)sender;
var items = listView.SelectedItems.ToArray();
if (items.Length > 0)
{
listView.DoDragDrop(items, DragDropEffects.Move);
}
}

You can adapt this part in many ways. The call to DoDragDrop() saves the data to drag and drop and specified which effect is allowed.

The sample above will start dragging only if there is one or more item selected. The ListView class has an event ItemDrag that is not implemented yet, but DragStart works for all controls in Wisej and you can decide how and what to drag.

Then to drop into listView1, or into anything else actually:

listView1.AllowDrop = true;

listView1.DragEnter += listView1.DragEnter;

listView1.DragDrop += listView1.DragDrop;

private void ListView1_DragEnter(object sender, DragEventArgs e)
{

     // here you can check the allowed effects specified in e decide what to respond. you can cancel drag or select the effect you want.
e.Effect = DragDropEffects.Move;
}

private void ListView1_DragDrop(object sender, DragEventArgs e)
{

    // here you can handle whatever data is being dragged and dropped.

   // in this case we saved an array of ListViewItem, but it can be anything.

   // and can decide if to move the items, clone them, change them, log, etc.

var items = (ListViewItem[])e.Data.GetData(typeof(ListViewItem[]));
if (items != null)
{
var listView = (ListView)sender;
foreach (var item in items)
{
item.Remove();
listView.Items.Add(item);
}
}
}

HTH

/Luca

  • conrad
    worked like a charm. thanks!
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.