01.
public
partial
class
frmDragNDrop_2 : Form
02.
{
03.
public
frmDragNDrop_2()
04.
{
05.
InitializeComponent();
06.
this
.dataGridView1.AllowDrop =
true
;
07.
this
.dataGridView2.AllowDrop =
true
;
08.
}
09.
private
int
rowIndexFromMouseDown;
10.
11.
private
void
frmDragNDrop_2_Load(
object
sender, EventArgs e)
12.
{
13.
for
(
int
i = 0; i < 10; i++)
14.
dataGridView1.Rows.Add(
"Test "
+ i,
"Tor "
+ i);
15.
}
16.
17.
private
void
dataGridView1_MouseDown(
object
sender, MouseEventArgs e)
18.
{
19.
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Copy);
20.
}
21.
22.
private
void
listBox2_DragDrop(
object
sender, System.Windows.Forms.DragEventArgs e)
23.
{
24.
DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(
typeof
(DataGridViewSelectedRowCollection));
25.
foreach
(DataGridViewRow row
in
rows)
26.
{
27.
try
28.
{
29.
listBox2.Items.Add(row.Cells[1].Value);
30.
}
31.
catch
{ }
32.
}
33.
34.
}
35.
36.
private
void
listBox2_DragEnter(
object
sender, DragEventArgs e)
37.
{
38.
if
(e.Data.GetDataPresent(
typeof
(DataGridViewSelectedRowCollection)))
39.
e.Effect = DragDropEffects.Copy;
40.
}
41.
42.
private
void
dataGridView2_DragDrop(
object
sender, DragEventArgs e)
43.
{
44.
DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(
typeof
(DataGridViewSelectedRowCollection));
45.
rowIndexFromMouseDown = dataGridView2.DragDropRowIndex(e);
46.
for
(
int
i = rows.Count-1; i >=0 ; i--)
47.
{
48.
if
(!
string
.IsNullOrEmpty(
""
+ rows[i].Cells[0].Value))
49.
dataGridView2.Rows.Add(rows[i].Cells[0].Value, rows[i].Cells[1].Value, rows[i].Cells[2].Value);
50.
}
51.
}
52.
53.
private
void
dataGridView2_DragEnter(
object
sender, DragEventArgs e)
54.
{
55.
Text =
"dataGridView2_DragEnter"
;
56.
if
(e.Data.GetDataPresent(
typeof
(DataGridViewSelectedRowCollection))) e.Effect = DragDropEffects.Copy;
57.
}
58.
59.
private
void
dataGridView2_DragOver(
object
sender, DragEventArgs e)
60.
{
61.
Text =
"dataGridView2_DragOver"
;
62.
if
(e.Data.GetDataPresent(
typeof
(DataGridViewSelectedRowCollection))) e.Effect = DragDropEffects.Copy;
63.
}
64.
65.
66.
}