Overriding OnMouseDown for DataGridView disables checkboxes even when calling parent method
I am working on winforms.NET 2008, I am trying to drag objects from DataGridView to some other control. So I had to override the OnMouseDown event handler. Since I have checkboxes, their state never changes. 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 into another control
Any help?
+1
a source to share
2 answers
OK, this is a hack, but it works. It inserts a slight delay in the drag, allowing the check / uncheck to work fine:
(edited to include cancelDrag
to avoid UI crashes 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);
}
}
});
}
});
}
+2
a source to share