views:

9

answers:

1

After I added drag & drop to a DataGridView, the CellDoubleClick event stopped working. In the CellMouseDown event I have the following code:

private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    var obj = dataGridView2.CurrentRow.DataBoundItem;
    DoDragDrop(obj, DragDropEffects.Link);
}

How do I correct this to enable CellDoubleClick event?

+1  A: 

Yes, that cannot work. Calling DoDragDrop() turns mouse control over to the Windows D+D logic, that's going to interfere with normal mouse handling. You need to delay starting the D+D until you see the user actually dragging. This ought to solve the problem:

    Point dragStart;

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
        if (e.Button == MouseButtons.Left) dragStart = e.Location;
    }

    private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            var min = SystemInformation.DoubleClickSize;
            if (Math.Abs(e.X - dragStart.X) >= min.Width ||
                Math.Abs(e.Y - dragStart.Y) >= min.Height) {
                // Call DoDragDrop
                //...
            }
        }
    }
Hans Passant
Thanks! That works well.
Max