Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Drag
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 11-Apr-13 9:14am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 319
2 CPallini 275
3 DamithSL 214
4 Maciej Los 185
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100