如何实现通过鼠标移动dataGridView中的行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何实现通过鼠标移动dataGridView中的行相关的知识,希望对你有一定的参考价值。
edwin5200回答的
IdDataSortIndex
是什么意思啊????
在线等。。。。。
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 (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
else
dragBoxFromMouseDown = Rectangle.Empty;
private void dataGridView1_DragOver(object sender, DragEventArgs e)
e.Effect = DragDropEffects.Move;
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
IdDataSortIndex(dataGridView1["ID", rowIndexOfItemUnderMouseToDrop].Value + "", dataGridView1["ID", rowIndexFromMouseDown].Value + "");
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow; dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown); dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
dataGridView1.Rows[rowIndexOfItemUnderMouseToDrop].Selected = true;
#endregion 实现行拖放
如果有什么不明白可联系我! 参考技术A region 实现行拖放
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 (dragBoxFromMouseDown ,= Rectangle.Empty && ,dragBoxFromMouseDown.Contains(e.X, e.Y))
DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown ,= -1)
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
else
dragBoxFromMouseDown = Rectangle.Empty;
private void dataGridView1_DragOver(object sender, DragEventArgs e)
e.Effect = DragDropEffects.Move;
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
IdDataSortIndex(dataGridView1["ID", rowIndexOfItemUnderMouseToDrop].Value + "", dataGridView1["ID", rowIndexFromMouseDown].Value + "");
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow; dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown); dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
dataGridView1.Rows[rowIndexOfItemUnderMouseToDrop].Selected = true;
#endregion 实现行拖放
如果有什么不明白可联系我。 参考技术B 在dataGridView中有鼠标移动事件MouseMove
以上是关于如何实现通过鼠标移动dataGridView中的行的主要内容,如果未能解决你的问题,请参考以下文章