A SearchWebServices.com member posed the following question to .NET Technologies expert Jim Culbert:
Q: Are there any good code examples for using drag and drop from a ListView to a DataGrid control? I'm building a standard Visual Basic.NET application and would like to implement this.
Here is Jim's reply:
Dunno. I'll make you one. Here goes.
Since you didn't ask about how to create or manipulate ListViews or DataGrids, I am going to assume for this reply that you already know how to do this. Once you've created your project with your ListView and DataGrid you need to do a couple of things to handle drag and drop events. The steps are:
The flow goes something like this. When an event occurs that your object decides is a drag start event your code responds by collecting up relevant data and passing it to the framework through your object's DoDragDrop method. The triggering event is usually the left mouse down event but some objects (like the ListView) implement special events to handle drag start. In the case of the ListView the object fires a special event called ItemDrag.
Now, while you drag the cursor around, the system is checking each of the objects that the cursor passes over to determine whether it is a drop target or not. The system takes care of your cursor management during the process switching pointers as you mouse from objects that implement the drop capability to those that do not. As you drag an item over objects, each object's DragEnter event is fired. How the object responds to this event determines whether a drop can be performed there. So, to support dropping data onto your DataGrid control, your control must have a DragEnter event handler setup. In this event handler, you can inspect the contents of the item being
To continue reading for free, register below or login
To read more you must become a member of SearchSOA.com
');
// -->

dragged around and you can decide whether you want to allow the drop or not. You signal your intentions to the framework through an object it passes into the event handler.
If your object signals that it will accept a drop, then when a user drops the item by releasing the mouse button, a DragDrop event is generated for your object. You need to handle this event to implement the drop. The framework hands the event handler the data that was setup by the ListView's ItemDrag event handler. Your object now has the data and can decide to do whatever it wants with it. In the case of the DataGrid, it is likely that you would stuff the data into the grid and possibly update the data source associated with the grid at the same time.
So that's the high-level bit, here is how you would implement it.
I've removed all the auto-generated code. This project is a simple Visual Basic Windows Forms application that has one form. I have instantiated a SQLConnection, SQLDataAdapter and a DataSet named SQLConnection1, SQLDataAdapter1 and DataSet1 respectively. Similarly, I've instantiated ListView1 and DataGrid1 as well. DataGrid1 is bound to DataSet1 so filling the dataset populates my DataGrid for me. You can choose to do this however you want. I've intentionally left that stuff out (as well as any error handling) to emphasize the drag and drop code.
Oh yeah, to enable drag and drop on your grid and view objects (step one mentioned at the beginning of the note), you can either do it in code or just flip the "AllowDrop" property to "True" for each object in their respective properties window in the Visual Studio IDE.
Happy Trails...
Code
Be sure to read more of Jim's answers. Pose your own .NET Technologies question to Jim today.