Hi Douglas,
The first thing you should do is to set the AllowDrop property of the
drag&drop target control to true. The next step is to handle the MouseMove
event of the drag&drop source and the DragEnter and DragDrop events of the
drag&drop target control.
I will illustrate how to do drag&drop operation between two DataGridView
controls with a sample. It requires that you create a WinForm application
project and add two DataGridView controls on the form. The two DataGridView
are called dataGridView1 and dataGridView2. The former is the drag&drop
source and the latter is the drag&drop target.
The following is the code in the form:
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView2.AllowDrop = true;
this.dataGridView1.MouseMove += new
MouseEventHandler(dataGridView1_MouseMove);
this.dataGridView2.DragEnter += new
DragEventHandler(dataGridView2_DragEnter);
this.dataGridView2.DragDrop += new
DragEventHandler(dataGridView2_DragDrop);
this.dataGridView1.RowCount = 3;
this.dataGridView1.Rows[0].Cells[0].Value = "1";
this.dataGridView1.Rows[0].Cells[1].Value = "11";
this.dataGridView1.Rows[1].Cells[0].Value = "2";
this.dataGridView1.Rows[1].Cells[1].Value = "22";
this.dataGridView1.Rows[2].Cells[0].Value = "3";
this.dataGridView1.Rows[2].Cells[1].Value = "33";
}
void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow))
as DataGridViewRow;
if (row != null)
{
DataGridViewRow newrow = row.Clone() as DataGridViewRow;
for (int i = 0; i < newrow.Cells.Count; i++)
{
newrow.Cells
.Value = row.Cells.Value;
}
this.dataGridView2.Rows.Add(newrow);
}
}
void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
e.Effect = DragDropEffects.Copy;
}
}
void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.dataGridView1.DoDragDrop(this.dataGridView1.CurrentRow,
DragDropEffects.All);
}
}
}
Build the project and run the application. Drag a row in the dataGridView1
and drop it onto the dataGridView2 and you should see this row is added
into the dataGridView2.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.