views:

501

answers:

2

Hi all,

I am working on .NET 2008 winforms, I am trying to drag and drop objects from a DataGridView to some other control. So I had to override the OnMouseDown event handler. Since I have checkboxes there, their state is never changed. Here's my overriden method

public class SeriesGrid : DataGridView
{

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        DataGridViewSelectedRowCollection selectedRows = this.SelectedRows;
        if (selectedRows.Count == 0) return;

        List<AppDataSeries> toDrag = new List<AppDataSeries>();
        for (int i = 0; i < selectedRows.Count; i++)
        {
            toDrag.Add((AppDataSeries)selectedRows[i].DataBoundItem);
        }
        this.DoDragDrop(toDrag, DragDropEffects.Copy);

    }

}

DoDragDrop seems to be causing the problem, because if I remove it the checkboxes work fine, however I don't get anything to drop in the other control

Any Help?

A: 

Try call base.OnMouseDown(e); at the end of the method.

leppie
I tried it. It doesn't work either!
Mustafa A. Jabbar
+2  A: 

OK, this is hacky - but it works. It inserts a small delay into the drag, allowing the check/uncheck to work normally:

(edited to include cancelDrag to help avoid some UI glitches with clicks)

volatile bool cancelDrag;
protected override void OnMouseUp(MouseEventArgs e)
{
    cancelDrag = true;
    base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);
    cancelDrag = false;
    ThreadPool.QueueUserWorkItem(delegate
    {
        Thread.Sleep(250);
        if (!cancelDrag)
        {
            BeginInvoke((MethodInvoker)delegate
            {
                if (!cancelDrag)
                {
                    // your code here...
                    var sel = this.SelectedRows;
                    if (sel.Count > 0)
                    {
                        this.DoDragDrop("test", DragDropEffects.All);
                    }
                }
            });
        }
    });
}
Marc Gravell
thanks Marc.. It works fine now. I appreciate your help
Mustafa A. Jabbar
No problem; and welcome to stackoverflow ;-p
Marc Gravell