如何在 DataGridView 中模仿 Windows 资源管理器的多选/拖放行为?
我正在尝试模仿 Windows 资源管理器处理多项选择的方式。在默认的 DataGridView 中,您可以使用 Ctrl 键单击来选择多个项目。但是,如果您释放 Ctrl 键,然后尝试拖放所选项目,则会清除所选项目并仅选择“命中”行。我在网上某处找到了以下解决方案。
protected override OnMouseDown(MouseEventArgs e)
{
int hitRowIndex = HitTest(e.X, e.Y).RowIndex;
if(!SelectedRows.Contains(Rows[hitRowIndex]))
{
base.OnMouseDown();
}
}
然而,这会导致其他副作用。按住 CTRL 键并将鼠标放在所选项目上,该项目将保持选中状态。这是有道理的,因为如果选择了单击的行,则将绕过 mousedown 事件。从 Windows 资源管理器的行为来看,按住 CTRL 键取消选择项目似乎直到 MouseUp 事件才得到处理。有人尝试这样做吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我创建了一个自定义组件来解决这个问题,以及我在 datagridview 中进行多重选择时遇到的其他一些恼人的问题。这是代码,希望对大家有帮助:
I have created a custom component to fix this, and some other annoying issues I had with multi selection in datagridview. This is the code, hope it helps anyone:
可能这对您有帮助:
May be this help you: