Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to do a drag and drop operation from a cell in a datagrid view to a text box. The operation appears to work but when the mouse is released nothing happens.

What I have tried:

private void dgv_Functions_MouseDown(object sender, MouseEventArgs e)
      {
          dgv_Functions.DoDragDrop(dgv_Functions.SelectedRows,
          DragDropEffects.Copy);
      }

private void txt_Query_DragEnter(object sender, DragEventArgs e)
      {
         e.Effect = DragDropEffects.Copy;
      }

private void txt_Query_DragDrop(object sender, DragEventArgs e)
      {
          if (e.Data.GetDataPresent(typeof(System.String)))
           {
              txt_Query.Text =
                  (System.String)e.Data.GetData(typeof(System.String));
           }
      }
Posted
Updated 27-Dec-22 1:03am
Comments
Graeme_Grant 26-Dec-22 12:13pm    
WPF or Winforms?
Member 13694735 26-Dec-22 12:22pm    
Winforms

 
Share this answer
 
Start with the debugger: put a breakpoint on the if line of your txt_Query_DragDrop method, and look at the e.Data object. Chances are that isn't not a string, but a DataGridViewCell or similar so your fetch fails to retrieve anything useful.

Once you know what you have got dropped, you can start coding round that.
 
Share this answer
 
Comments
Member 13694735 26-Dec-22 12:43pm    
I did this but the txt_Query_DragDrop method is not executed.
in dgv_Functions_MouseDown, you are sending the value of "dgv_Functions.SelectedRows" for the "drag".

on the other hand, at the target drop landing, txt_Query_DragDrop is expecting to receive a value of string for the "drop".

Maybe "SelectedRows" is not a "string". or is it?
 
Share this answer
 
v2

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