Drag and Drop in C# Does Not Work

Steve Clark 21 Reputation points
2022-12-27T19:59:37.41+00:00

I have a C# program that is attempting to do a drag and drop operation. The pro gram is trying to drag from data in a datagridview and drop it to a textbox. The datagridview is a single cell.

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

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

What happens the drag appears to work but the drop appears to but nothing is dropped. Can some explain to me why this does not work.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-12-28T02:16:50.74+00:00

    @Steve Clark , Welcome to Microsoft Q&A, we also need to set the textBox1_DragDrop event if we want to drag data from datagirdview to textbox.

    Here is a code example you could refer to.

     private void Form1_Load(object sender, EventArgs e)  
            {  
                List<Student> list = new List<Student>();  
                list.Add(new Student { Name="test1" });  
                dataGridView1.DataSource=list;  
                dataGridView1.AllowDrop= true;  
                textBox1.AllowDrop= true;  
                 
            }  
      
            private void textBox1_DragEnter(object sender, DragEventArgs e)  
            {  
                if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))  
                {  
                    e.Effect = DragDropEffects.Move;  
                }  
            }  
            private void dataGridView1_MouseDown(object sender, MouseEventArgs e)  
            {  
                dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);  
            }  
            private void textBox1_DragDrop(object sender, DragEventArgs e)  
            {  
                DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));  
      
                foreach (DataGridViewRow row in rows)  
                {  
                    textBox1.Text = row.Cells[0].Value.ToString();  
                     
                }  
            }  
    

    Note: Please select the datagirdview row first of all and do the dragdrop operation.

    Tested result:

    274379-animation.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Steve Clark 21 Reputation points
    2022-12-30T21:24:26.787+00:00

    Please understand I am a beginner in C# and do not understand. I copied the code from your example to my PC and nothing happens. Could you explain it in simpler terms and provide an example? I have provided a copy of my code below. Thank you.

      public Form1()  
            {  
                InitializeComponent();  
            }  
            public class Student  
            {  
      
                public string Name { get; set; }  
      
            }  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                List <Student> list = new List<Student>();  
                list.Add(new Student { Name = "test1" });  
                dgv_Functions.DataSource = list;  
                dgv_Functions.AllowDrop = true;  
                textBox1.AllowDrop = true;  
            }  
      
            private void textBox1_DragEnter(object sender, DragEventArgs e)  
            {  
                if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))  
                {  
                    e.Effect = DragDropEffects.Move;  
                }  
            }  
            private void dataGridView1_MouseDown(object sender, MouseEventArgs e)  
            {  
                dgv_Functions.DoDragDrop(dgv_Functions.SelectedRows, DragDropEffects.Move);  
      
            }  
            private void textBox1_DragDrop(object sender, DragEventArgs e)  
            {  
                DataGridViewSelectedRowCollection rows =   
                (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));  
      
                foreach (DataGridViewRow row in rows)  
                {  
                    textBox1.Text = row.Cells[0].Value.ToString();  
      
                }  
            }  
        }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.