datagridviews
我最近完成了我的两个之间的拖放事件main form
,并且效果很好。但是,现在我决定进行更改,需要datagridview2
在另一个表单 ( form2
) 上。谁能告诉我如何在两个之间拖放datagridviews
,而分开forms
?
以下是我现有的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
dataGridView1.Rows[dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex].Cells[dataGridView1.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex].Value = (System.String)e.Data.GetData(typeof(System.String));
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
dataGridView2.DoDragDrop(
dataGridView2.Rows[e.RowIndex]
.Cells[e.ColumnIndex]
.Value.ToString(),
DragDropEffects.Copy);
}