Reputation: 11
I have recently finished my drag drop event between two datagridviews
on my main form
, and it works well. Now, however, I have decided to make a change that requires datagridview2
to be on another form (form2
). Can anyone tell me how to drag drop between two datagridviews
, while on separate forms
?
Below is my existing code:
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);
}
Upvotes: 1
Views: 1009
Reputation: 335
I my opinion drag and drop between two different forms work same like on one form. Are you sure that dataGridView1 property AllowDrop is set to True?
Upvotes: 1