کپی محتویات یک سلول از دیتاگرید در سلول دیگر با Drag&Drop
یک برنامه ویندوزی ایجاد کرده و یک DataGridView بر روی آن قرار دهید. خاصیت AllowDrop دیتاگرید را برابر True قرار دهید. سپس کدهای زیر را در رویدادهای مربوطه بنویسید :
using System; using System.Drawing; using System.Windows.Forms; namespace DragNDrop { public partial class Form1 : Form { public Form1() { InitializeComponent(); } 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" }); } private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Left) { dataGridView1.DoDragDrop(dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue, DragDropEffects.Copy); } } private void dataGridView1_DragDrop(object sender, DragEventArgs e) { string cellvalue = e.Data.GetData(typeof(string)) as string; Point cursorLocation = this.PointToClient(new Point(e.X, e.Y)); DataGridView.HitTestInfo hittest = dataGridView1.HitTest(cursorLocation.X, cursorLocation.Y); if (hittest.ColumnIndex != -1 && hittest.RowIndex != -1) dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue; } private void dataGridView1_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } } }
حال یکی محتویات یکی از سلول ها رو به دداخل سلول دیگر Drag کنید :