Click here to Skip to main content
15,890,690 members
Articles / Programming Languages / C#
Article

Drag Drop Text from DataGridView to RichTextBox or TextBox

Rate me:
Please Sign up or sign in to vote.
3.71/5 (9 votes)
19 Apr 2007CPOL1 min read 90.9K   2.7K   40   5
How to drag/drop text from datagridview control to richtextbox or textbox
Screenshot - behaviorRichTextBox.jpg

Introduction

In this article, we will learn how to drag a cell's text from a DataGridView and drop it to a Textbox or RichTextBox control.

Using the Code

In the drag operation of text from the DataGridView cell, the following events participate:

dataGridView1_MouseDown

This event occurs when the mouse pointer is over the control and a mouse button is pressed. We use this event to capture the cell's text which is being dragged from the DataGridView. The following line of code accomplishes this:

C#
//
//   if (e.Button == MouseButtons.Left)
//          {
//              DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
//              if (info.RowIndex >= 0)
//              {
//                  if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
//                  {
//                      string text = (String)
//                       dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
//                      if (text != null)
//                          dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
//                  }
//              }
//          }
//

DataGridView.HitTestInfo contains information such as the row and column indexes about a specific coordinate pair in the DataGridView control. This class cannot be inherited.

This is all that you have to write on DataGridView's events.

Drop a Text on TextBox

textBox1_DragDrop

This event occurs when the mouse pointer is on the textbox control and mouse button is released. You have to typecast the data from the DragEventArgs to assign it to the text property of the textbox.

C#
//private void textBox1_DragDrop(object sender, DragEventArgs e)
//{
//    if (e.Data.GetDataPresent(typeof(System.String)))
//    {
//        textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
//    }
//}

textBox1_DragEnter

This event occurs when the mouse pointer is dragged and it enters in the textbox. Here you have to specify the effects of the drag-drop operation.

C#
//private void textBox1_DragEnter(object sender, DragEventArgs e)
//{
//  e.Effect = DragDropEffects.Copy;
//}

Other than this, you also have to set the AllowDrop property of the textbox to true.

Drop a Text in RichTextBox

To drop a text in the RichTextBox, you only have to set its EnableAutoDragDrop property to true.

History

  • 19th April, 2007: Initial post

License

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


Written By
Software Developer (Senior)
Australia Australia
10+ years of Progamming Experience in C++,vb6.0,Delphi 5 & 7,VB.Net,C#.Net & ASP.Net
MCP for Windows Application Development using C#
MCP for Web based Application Development using C#

Comments and Discussions

 
Suggestionsmall changes to the source code Pin
keibel23-Sep-14 22:38
keibel23-Sep-14 22:38 
GeneralRichtextbox DragDrop event is not getting triggered in few system Pin
Padoor Shiras17-Mar-09 19:59
Padoor Shiras17-Mar-09 19:59 
GeneralCombobox columns Pin
Peter Ritchie20-Jun-08 10:03
Peter Ritchie20-Jun-08 10:03 
What about drag-drop of a combobox column?


Questiona msg from nvmsabarinath Pin
Sean Ewington25-Apr-07 5:34
staffSean Ewington25-Apr-07 5:34 
AnswerRe: a msg from nvmsabarinath Pin
Nouman Bhatti25-Apr-07 18:56
Nouman Bhatti25-Apr-07 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.