我正在尝试在 DataGridView 中实现行移动。我希望能够选择多行并单击任何选定行的单元格以开始拖动操作。问题是当我将鼠标放在单元格上时,行会被取消选择。我怎样才能防止这种情况发生?
问问题
4025 次
3 回答
2
从快速的谷歌搜索来看,这似乎是自定义拖放行的解决方案。请注意,我刚刚从链接页面中提取了以下代码,我无法保证其有效性。
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);
}
}
于 2010-07-03T20:11:55.790 回答
2
对 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;
}
};
于 2016-03-12T18:01:56.173 回答
0
我个人不确定如何更改您正在谈论的问题的默认行为,但我知道默认情况下右键单击不会对 DataGridView 执行任何操作。话虽如此,解决方法可能是实现我以前做过的事情:自定义 ContextMenuStrip 允许用户通过选择行来复制/粘贴行,右键单击以打开上下文菜单,复制行,然后右键 -单击行标题以打开上下文菜单并粘贴。CellClick 和 RowHeaderMouseClick 事件应该使这更容易。
于 2010-07-03T18:43:18.440 回答