How do I mimic Windows Explorer multi-select/drag-n-drop behavior in a DataGridView?
Posted
by
bsh152s
on Stack Overflow
See other posts from Stack Overflow
or by bsh152s
Published on 2009-12-21T21:06:43Z
Indexed on
2010/12/22
4:54 UTC
Read the original article
Hit count: 205
I'm trying to mimic the way Windows Explorer handles multiple selection. In a default DataGridView, you can select multiple items using Ctrl-click. But if you release the Ctrl key and then try and drag/drop the selected items, it clears the selected items and only selects the "hit" row. I found the following solution somewhere online.
protected override OnMouseDown(MouseEventArgs e)
{
int hitRowIndex = HitTest(e.X, e.Y).RowIndex;
if(!SelectedRows.Contains(Rows[hitRowIndex]))
{
base.OnMouseDown();
}
}
However, this causes other side effects. With the CTRL key pressed and mousing down on a selected item, the item remains selected. This makes sense because the mousedown event is bypassed if the row clicked on is selected. From looking at the behavior of Windows Explorer, it looks like the deselection of an item with the CTRL key held is not handled until the MouseUp event. Has anyone tried to do this?
© Stack Overflow or respective owner