Problem solve Get help with specific problems with your technologies, process and projects.

Using drag and drop from a ListView to a DataGrid control

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.
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:

1. Enable drag and drop capabilities in your grid and view.
2. Implement an event handler for your ListView that handles the drag start event.
3. Implement an event handler for your DataGrid that handles the drag enter event.
4. Implement an event handler for your DataGrid that handles the drop event.

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 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.

Read the rest of this response in our .NET Developer Tips section, which includes a code sample.

Dig Deeper on Topics Archive

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.