使用两个datagridviews c#在两个表单之间拖放

时间:2013-07-04 12:12:55

标签: c# .net visual-studio

我最近在我的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);
    }

1 个答案:

答案 0 :(得分:1)

我认为两种不同形式之间的拖放工作与一种形式相同。您确定dataGridView1属性AllowDrop设置为True吗?

相关问题