Tagamoga
Tagamoga

Reputation: 360

Listviews And Drag and Drop in C#

I would like to use drag and drop between two Listviews (AllListView and PreListView). This is how far I did get:

In the function where the AllListView is filled with Items, I use something like that to assosiate the myCustomDataObject to the single ListviewItem:

ListViewItem newItem = new ListViewItem();
newItem.Text = myCustomDataObject.getName();
newItem.Tag = myCustomDataObject;
lst_All.Items.Add(newItem);

There are my Eventhandler for the two Listviews:

AllListView:

private void OnAllDragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
    // How Do I add my CustomDataObject?
}

private void OnAllItemDrag(object sender, ItemDragEventArgs e)
{
    base.DoDragDrop(lst_All.SelectedItems[0], DragDropEffects.Move);
    // Do I have to Do something to pass my CustomDataObject?
}

PreListView:

private void OnPreDragEnter(object sender, DragEventArgs e)
{
    //If there one of myCustomDataObject go on
    e.Effect = DragDropEffects.Move;
}

private void OnPreDragDrop(object sender, DragEventArgs e)
{
    // Get Here myCustomDataObject to generate the new Item
    lst_Pre.Items.Add("Done...");
}

So my question is, how to achieve that myCustomDataObject is found in “OnPreDragDrop”. I have tried many versions of e.Data.Getdata() and e.Data.Setdata(), but I did not got very far.

Upvotes: 0

Views: 3711

Answers (3)

Hans Passant
Hans Passant

Reputation: 941218

You are dragging an object of type ListViewItem. So you first want to check that the dragged item is indeed of that type. And you probably want to make sure it is a happy kind of item, one that has the proper Tag value. Thus:

private void OnPreDragEnter(object sender, DragEventArgs e) {
    if (e.Data.GetDataPresent(typeof(ListViewItem))) {
        var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
        if (item.Tag is CustomDataObject) {
            e.Effect = DragDropEffects.Move;
        }
    }
}

In the Drop event, you actually want to implement the logical "Move" operation, removing the item from the source ListView and adding it to the destination ListView. No need for checks anymore, you already performed them in your DragEnter event handler. Thus:

private void OnPreDragDrop(object sender, DragEventArgs e) {
    var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
    item.ListView.Items.Remove(item);
    lst_Pre.Items.Add(item);
}

Note that you probably thought for a minute that the mistake was to drag the ListViewItem instead of the CustomDataObject. It was not, dragging ListViewItem made it easy to remove the item from the source ListView.

Upvotes: 5

DonBoitnott
DonBoitnott

Reputation: 11025

When you call DoDragDrop, you are assigning the data. Make that your custom data object instead of the ListViewItem.

If you need the ListViewItem, then add a reference to it to your custom data class.

Upvotes: 0

tarzanbappa
tarzanbappa

Reputation: 4958

List view normally doesn't have drag and drop facility. But you can make it to do drag and drop with some changes with some extra code. Here is a link to help your problem. I hope you'll get something from it.

http://support.microsoft.com/kb/822483

Upvotes: 0

Related Questions