اضافه کردن محتویات یک سطر از DataGridView به ListBox با استفاده از Drag&Drop
یک برنامه ویندوزی ایجاد کرده و یک کنترل ListBox و DataGridView به آن اضافه کنید.
خاصیت AllowDrop کنترل ListBox را به true تغییر دهید و کدهای زیر را در رویدادهای مربوطه بنویسید :
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication72 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.ColumnCount = 2; this.dataGridView1.Rows.Add(new object[] { "A", "B" }); this.dataGridView1.Rows.Add(new object[] { "C", "D" }); } private void dataGridView1_MouseDown(object sender, MouseEventArgs e) { dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move); } private void listBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection))) { e.Effect = DragDropEffects.Move; } } private void listBox1_DragDrop(object sender, DragEventArgs e) { DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection)); foreach (DataGridViewRow row in rows) { listBox1.Items.Add(row.Cells[0].Value); listBox1.Items.Add(row.Cells[1].Value); dataGridView1.Rows.Remove(row); } } } }
حال برنامه را اجرا و یک سطر از DataGrid را انتخاب کنید و آن را به داخل ListBox بکشید :