Click here to Skip to main content
Click here to Skip to main content
Go to top

Drag Drop Text from DataGridView to RichTextBox or TextBox

, 19 Apr 2007
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

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)

Share

About the Author

Nouman Bhatti
Software Developer (Senior)
Pakistan Pakistan
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#

Comments and Discussions

 
GeneralRichtextbox DragDrop event is not getting triggered in few system PinmemberPadoor Shiras17-Mar-09 19:59 
GeneralCombobox columns PinmemberPeter Ritchie20-Jun-08 10:03 
Questiona msg from nvmsabarinath PinstaffSean Ewington25-Apr-07 5:34 
AnswerRe: a msg from nvmsabarinath PinmemberNouman Bhatti25-Apr-07 18:56 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Nouman Bhatti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid