C# 如何在彼此下方拖放 DataGridView 行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1620947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could I Drag and Drop DataGridView Rows under each other?
提问by Wahid Bitar
I've DataGridView
that bound a List<myClass>
and i sort it by "Priority
" property in "myClass
".
So I want to drag an "DataGridViewRow
" to certain position to change it's "Priority
" property.
我已经DataGridView
绑定了 aList<myClass>
并按“ Priority
”中的“ ”属性对其进行了排序myClass
。所以我想拖一个“ DataGridViewRow
”到某个位置来改变它的“ Priority
”属性。
How could I "Drag & Drop" DataGridView Rows ?.And how to handle this ?.
我怎么能“拖放”DataGridView 行?以及如何处理这个?
采纳答案by Wahid Bitar
I found this code sampleon MSDN
我在 MSDN 上找到了这个代码示例
Note the following:
请注意以下事项:
1). DataGridView property AllowDrop must be set to true (default is false).
1)。DataGridView 属性 AllowDrop 必须设置为 true(默认为 false)。
2). The example below works out of the box when the DataGridView is NOTdata-bound. Otherwise it will throw an InvalidOperationException. If it is databound, you should manipulate the order of items in the DataSource
.
2)。当 DataGridView不是数据绑定时,下面的示例开箱即用。否则它会抛出一个 InvalidOperationException。如果它是数据绑定的,您应该操作DataSource
.
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);
}
}
回答by Wilhelm
The Answer from 'Wahid Bitar' helped me a lot. I cannot comment the above, so a small addition: If deleting a row is unwanted as commented by 'neminem': Just add to DragDrop Event just before the lines that finally moves it:
'Wahid Bitar' 的回答对我帮助很大。我无法评论以上内容,所以要补充一点:如果删除一行是不需要的,正如 'neminem' 评论的那样:只需在最终移动它的行之前添加到 DragDrop 事件:
if (rowIndexOfItemUnderMouseToDrop < 0 )
{
return;
}
dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);