Home > SOA Tips > .NET Developer > Using drag and drop from a ListView to a DataGrid control
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Using drag and drop from a ListView to a DataGrid control


Jim Culbert, CTO, MetraTech
07.11.2003
Rating: -2.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Microsoft .NET Web services
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Silverlight shines on bank RIAs
Microsoft .NET Web services Research

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


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.


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts