Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
There are tons of examples about how to receive a drag/drop event into a control in a C# application. However, I need to send data OUT of my application via drag/drop.

Specifically, I have a DataGridView that lists a set of files. The files can be anything - Excel, Word, BMP, you name it and my application allows the user to import it, and see a list of what's been imported.

I need for the user to be able to drag a file out of my DataGridView and into another (external) application. I have all the basic code in place to do the drag/drop (right now it is sending a file path as a String), but I can't figure out how to wrap the actual file data into the correct object form for the DoDragDrop event.

Currently, I write the selected files to a TMP folder on drag/drop initiation, so the reference is to an actual file on disk. However, I have the binary for each file stored in a database, so it is perfectly acceptable if the solution takes data directly from the database.
Posted

I'm not sure how drag/drop would be confusing. In any case, that is the requirement that I must code to.

After much thought and research, I stumbled upon an idea: I went ahead and implemented standard drag/drop TO my DataGridView, and debugged through the app to see what kind of data was being submitted. This led me to the OleDataObject class, which can be implemented simply as System.Windows.Forms.DataObject. Now, the core of the code (which I've simplified for illustration) becomes...

// You may not need all these USING statements, but I did not take time to filter out only the ones related to this example.
				
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

	String strTmpFileName = Path.GetTempFileName();// Get a temp file to write to
						strTmpFileName = Path.ChangeExtension(strTmpFileName, doc.DocType);// Change file extension from "TMP" to appropriate type so that the appropriate file association will be found
						DataObject dragObject = new System.Windows.Forms.DataObject();
						StringCollection filePaths = new StringCollection();
						filePaths.Add(strTmpFileName);
						dragObject.SetFileDropList(filePaths);


This code sets a filepath into the drag object, which can then be passed to the DoDragDrop function. Make sure the file exists before a drag operation takes place. I do this by writing a TMP file as soon as the user selects a row in my DataGridView.
 
Share this answer
 
Comments
Member 14945412 22-Sep-20 3:56am    
Signed up just to say thank you for following up on this!
I think the partial answer would be to use the clipbord.
Clipboard handling with .NET[^]

I woudnt use the dragdrop routine at all though, as I think it would be more confusing to people.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900