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