Sunday, February 21, 2010

Drag and Drop text from one to other datagrirdview in C# windows Application

AFTER reading lots of articles on various sites i get to knowthat it's easy to add a row or column to DataGridView Control using dragdrop. but there is problem when we have to add data to a single cell. because during drag of data we are unable to get cells rowIndex and ColumnIndex. so we have to determine using the X and Y Coordinates returned  by mouse pointer.
Using the code

there are only three main event that u have to handle.

1. CellMouseDown for the DataGridView (dataGridView2 in my case) from which data is copied.

and we write following code :  

private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
 dataGridView2.DoDragDrop(dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), DragDropEffects.Copy);
}  


2. DragEnter event of the DataGridView (dataGridView1 in My case) in which data is to be paste.

       private void dataGridView1_DragEnter(object sender, DragEventArgs e)
       {
           if (e.Data.GetDataPresent(typeof(System.String)))
               e.Effect = DragDropEffects.Copy;
           else
               e.Effect = DragDropEffects.None;
       }


Read more: Codeproject

Posted via email from jasper22's posterous