0

I have a C# WinForms application with a DataGridView control that has few DataGridViewTextBoxCell cells.

I would like to be able to drag each cell to some external application while showing the contents of the item (either a text or an image of this text. Even a properly sized rectnagle could be acceptable).

I was able to make the drag and drop to another window and it does copy the value of the dragged item in the target window, but it does not display anything while dragging. Moreover, when I start the drag, it shows a forbidden icon but it still drags and copies the value to the target window.

mmmmmm
  • 980
  • 1
  • 14
  • 16
  • 1
    This is not easy I think. [See here](http://stackoverflow.com/questions/31193787/net-drag-and-drop-show-dragged-borders-or-image-such-as-windows-do/31212867#31212867) for a similar question with a discussion of the issues.. - I fact not using drag&drop but mouse events will probably make it a lot easier. – TaW May 05 '17 at 11:53
  • Thank you for the link. It seems indeed the right approach. Now, I only need to get hold of the TextBox control that is hosted in the cell. I am haing a bit of a problem in obtaining the TextBox control. – mmmmmm May 08 '17 at 06:34
  • 1
    While one can get hold of that TBox and even set some of its properties I doubt you can move it or actually even just keep it visible once you start drgging. As soon as editing is ended it will be hidden. Instead you can either just grab the text and stuff it into a label with the same styles or, if you do complicated stuff in cellpainting, you could take a snapshot with drawtobitmap, cut it down to the cell (or row..) and put it into a label or panel.. - The TBox is an 'Editingcontrol' btw but only useful while editing. – TaW May 08 '17 at 06:39
  • 1
    [This post](http://stackoverflow.com/questions/3240603/c-sharp-drag-and-drop-show-the-dragged-item-while-dragging) may have worked it out. I had posted an example for using mouse event until I noticed you actually want to drop cell values to outside applications, not moving rows inside the DGV. – TaW May 08 '17 at 10:01
  • Thank you for posting.Using this: https://blogs.msdn.microsoft.com/adamroot/2008/02/19/shell-style-drag-and-drop-in-net-part-2/ I am really close in achiving it, but it shows the image only when outsite de my own window. – mmmmmm May 08 '17 at 10:42
  • 1
    Interesting. And involved.. - You could try to catch the Form.Leave or, since it won't happen while the LeftButton is down catch the fact that the mouse is leaving the form (maybe in a timer). For the time before you can show a label with the cell data and start the d&d only when you leave.. – TaW May 08 '17 at 11:01
  • Great ideea. It is indeed another aproach that seems somehow simpler than the path I went on. I will give it a try and come back with an update if successfull. – mmmmmm May 08 '17 at 11:54
  • yes, but you might need to combine both.. – TaW May 08 '17 at 12:00

0 Answers0