Click here to Skip to main content
12,956,512 members (77,825 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 19 Apr 2007

Drag Drop Text from DataGridView to RichTextBox or TextBox

, 19 Apr 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to drag/drop text from datagridview control to richtextbox or textbox
Screenshot - behaviorRichTextBox.jpg


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:


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:

//   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


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.

//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));
//    }


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.

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


  • 19th April, 2007: Initial post


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


About the Author

Nouman Bhatti
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#

You may also be interested in...

Comments and Discussions

Suggestionsmall changes to the source code Pin
keibel23-Sep-14 22:38
memberkeibel23-Sep-14 22:38 
GeneralRichtextbox DragDrop event is not getting triggered in few system Pin
Padoor Shiras17-Mar-09 19:59
memberPadoor Shiras17-Mar-09 19:59 
GeneralCombobox columns Pin
Peter Ritchie20-Jun-08 10:03
memberPeter Ritchie20-Jun-08 10:03 
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
memberNouman 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Nouman Bhatti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid