我想实现在DataGridView中移动的行。我希望能够选择多行并单击任何选定行的单元格以开始拖动操作。问题是当我将鼠标放在单元格上时,这些行会被取消选中。我怎样才能防止这种情况发生?如何在鼠标按住单元格时在DataGridView中维护选定的行?
3
A
回答
2
从快速谷歌搜索,this似乎是自定义拖放行的解决方案。注意我只是从链接页面中撕下下面的代码,我无法保证其有效性。
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
}
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
// Remember the point where the mouse down occurred.
// The DragSize indicates the size that the mouse can move
// before a drag event should be started.
Size dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width/2), e.Y - (dragSize.Height/2)), dragSize);
}
else
{
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty;
}
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
// Get the row index of the item the mouse is below.
rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
// If the drag operation was a move then remove and insert the row.
if (e.Effect== DragDropEffects.Move)
{
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
}
}
0
我个人不确定如何更改您所说的问题的默认行为,但我知道默认情况下,右键单击不会对DataGridView执行任何操作。有了这个说法,解决方法可能会实现我之前做的一些事情:自定义的ContextMenuStrip允许用户通过选择行来复制/粘贴行,右键单击打开上下文菜单,复制行,然后右键单击,单击行标题以打开上下文菜单并粘贴。 CellClick和RowHeaderMouseClick事件应该使这更容易。
1
子类DataGridView的帮助你做一个有条件的办法:
class SimpleDataGridView : DataGridView {
public Action<DataGridViewCellMouseEventArgs> BeforeCellMouseDown;
public Action<DataGridViewCellMouseEventArgs> AfterCellMouseDown;
protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e) {
if(BeforeCellMouseDown != null)
BeforeCellMouseDown(e);
base.OnCellMouseDown(e);
if(AfterCellMouseDown != null)
AfterCellMouseDown(e);
}
}
然后,您可以使用这种方式,在构造函数:
IEnumerable<DataGridViewRow> sel = null;
dataGridView1.BeforeCellMouseDown =
e => {
if (yourCondition)
// Save the selection
sel = dataGridView1.SelectedRows.OfType<DataGridViewRow>();
else
sel = null;
};
dataGridView1.AfterCellMouseDown =
e => {
if(sel != null) {
// Restore the selection
foreach(var row in sel)
row.Selected = true;
}
};
相关问题
- 1. 如何在按住鼠标的同时移动表单?
- 2. 在datagridview中维护当前选定的行
- 3. 如何在按住按钮的同时按住所选颜色来更改单元格
- 4. 如何防止在选择多个datagridview行时选择“新”datagridview行中的单元格?
- 5. iOS Swift:在单元格中维护切换按钮状态
- 6. 如何在DataGridView中更改单元格值时进行监视?
- 7. 防止在DataGridView中选择单元格
- 8. 如何在DataGridView中编辑单元格?
- 9. 在AutoHotkey中按住鼠标左键
- 10. 如何将行鼠标移至行中的所有单元格
- 11. 按住鼠标按钮时更改mouseListener
- 12. 如何维护鼠标悬停状态?
- 13. 如何在单击该单元格中的按钮时更改自定义单元格中的标签文本
- 14. 将选定的datagridview单元格存储在C#中的数组#
- 15. 总结DataGridview中选定的单元格时出现的问题
- 16. 如何检查Visual Basic中选定单元格的DataGridView列的标题文本
- 17. 在DataGridView中选择新行的第一个可见单元格
- 18. DataGridView鼠标选择
- 19. 如何保护Excel中选定的单元格使用的NodeJS
- 20. 如何添加复选框和标签在datagridview的同一个单元格中?
- 21. 在选定行中编辑单元格
- 22. 在选定单元格中运行宏
- 23. 如何识别在DataGridView中悬停的特定单元格
- 24. 如何确定在datagridview中的哪些单元格被检查?
- 25. 如何找到一行在dataGridView单元格中的位置?
- 26. 如何在选中单元格后停止DataGridView编辑?
- 27. 如何在DataGridView中选择多个单元格
- 28. 如果某个选定的单元格在第2行,请在datagridview中选择第1行的值
- 29. 维护鼠标悬停在jquery上的悬停菜单
- 30. 编辑datagridview单元格时;用鼠标点击任意位置的光标
我不认为这解决了这个问题:“问题在于,当我将鼠标放在单元格上时,这些行会被取消选择。”我也在寻找解决这个问题的办法。 – cdavidyoung 2012-09-21 17:43:59