Click here to Skip to main content
Licence CPOL
First Posted 19 Apr 2007
Views 49,292
Downloads 953
Bookmarked 39 times

Drag Drop Text from DataGridView to RichTextBox or TextBox

By | 19 Apr 2007 | Article
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:

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

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

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

About the Author

Nouman Bhatti

Software Developer (Senior)

Pakistan Pakistan

Member

7+ 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#

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRichtextbox DragDrop event is not getting triggered in few system PinmemberPadoor Shiras19:59 17 Mar '09  
GeneralCombobox columns PinmemberPeter Ritchie10:03 20 Jun '08  
Questiona msg from nvmsabarinath PinstaffSean Ewington5:34 25 Apr '07  
AnswerRe: a msg from nvmsabarinath PinmemberNouman Bhatti18:56 25 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Nouman Bhatti
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid