Drag&Drop مقدار سلول یک DataGridView در سلول DataGridView دیگر

یک برنامه ویندوزی ایجاد کرده و به صورت زیر دو دیتاگرید بر روی آن قرار دهید:
drag-and-drop-cell-value-of-one-datagridview-into-another-datagridview-cell-csharp-01

نکته :خاصیت AllowDrop هر دو دیتاگرید رو برابر True بذارید.
حال کدهای زیر را در رویدادهای مربوطه بنویسید :

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DragNDrop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Rectangle dragBoxFromMouseDown;
        private object valueFromMouseDown;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.ColumnCount = 3;
            this.dataGridView1.Rows.Add(new object[] { "A", "B" });
            this.dataGridView1.Rows.Add(new object[] { "C", "D" });

            this.dataGridView2.ColumnCount = 3;
            this.dataGridView2.Rows.Add(new object[] { "", "" });
            this.dataGridView2.Rows.Add(new object[] { "", "" });
        }


        private void dataGridView2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
                {                 
                    DragDropEffects dropEffect = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy);
                }
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            var hittestInfo = dataGridView1.HitTest(e.X, e.Y);

            if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
            {
                valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex].Value;
                if (valueFromMouseDown != null)
                {       
                    Size dragSize = SystemInformation.DragSize;
                    dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
                }
            }
            else
                dragBoxFromMouseDown = Rectangle.Empty;
        }

        private void dataGridView2_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void dataGridView2_DragDrop(object sender, DragEventArgs e)
        {
            Point clientPoint = dataGridView2.PointToClient(new Point(e.X, e.Y));
            if (e.Effect == DragDropEffects.Copy)
            {
                string cellvalue = e.Data.GetData(typeof(string)) as string;
                var hittest = dataGridView2.HitTest(clientPoint.X, clientPoint.Y);
                if (hittest.ColumnIndex != -1
                    && hittest.RowIndex != -1)
                    dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;

            }
        }
    }
}

برنامه را اجرا کرده و یک مقدار از دیتاگرید اول را به سلولی از دیتاگرید دوم درگ کنید :
drag-and-drop-cell-value-of-one-datagridview-into-another-datagridview-cell-csharp-02