当鼠标按住单元格时,如何在 DataGridView 中维护选定的行?
我正在尝试在 DataGridView 中实现行移动。我希望能够选择多行并单击任何选定行的单元格以开始拖动操作。问题是当我将鼠标放在单元格上时,行会被取消选择。我怎样才能防止这种情况发生?
I am trying to implement row moving in a DataGridView. I want to be able to select multiple rows and click on any of the selected row's cells to begin the drag operation. The problem is that the rows become deselected when I hold the mouse down on a cell. How can I prevent this from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
子类化 datagridview 可以帮助您以有条件的方式执行此操作:
然后,您可以在构造函数中以这种方式使用它:
Subclassing the datagridview helps you to do it a conditional way :
Then, you can use it this way, in your constructor:
通过快速 Google 搜索,这似乎是自定义行拖放的解决方案。请注意,我刚刚从链接页面中删除了以下代码,我无法保证其有效性。
From a quick Google search, this seems to a solution for custom drag-dropping of rows. Note I just ripped the following code from the linked page, I can't vouch for its effectiveness.
我个人不确定如何更改您正在谈论的问题的默认行为,但我确实知道默认情况下右键单击不会对 DataGridView 执行任何操作。话虽如此,解决方法可能是实现我之前做过的事情:自定义 ContextMenuStrip 允许用户通过选择行来复制/粘贴行,右键单击打开上下文菜单,复制行,然后右键-单击行标题打开上下文菜单并粘贴。
CellClick 和 RowHeaderMouseClick 事件应该使这变得更容易。
I'm personally not sure how to change the default behavior of the problem you're talking about, but i do know that by default a right-click doesn't do anything on the DataGridView. With that said, a work-around might be do implement something I've done before: a custom ContextMenuStrip allowing users to Copy/Paste rows by selecting the rows, right-clicking to open the context menu, copy the rows, then right-click a row header to open the context menu and paste.
The CellClick and RowHeaderMouseClick events should make this easier.