我已經複製了一些代碼並進行了修改以適合我的應用程序。我會繼續調整和清理代碼,直到我對它滿意爲止。但是我遇到了一點錯誤。我有兩個datagridviews,並希望將datagridrows從一個移動到另一個。但是,雖然drag&drop
事件全部觸發,但dataGridView_Routes_DragDrop()
將執行日誌命令,因爲e.Data.GetData
中沒有數據。我做錯了什麼?我錯過了什麼嗎?我試圖查看幾個指南,但沒有具體說明這個問題。在DataGridView之間拖放
我怎樣才能讓datagrid將拖動的datagridrow傳遞給其他datagrid?
/* Drag & Drop */
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private void dataGridView_Trips_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 = dataGridView_Trips.DoDragDrop(dataGridView_Trips.Rows[rowIndexFromMouseDown], DragDropEffects.Copy);
}
}
}
private void dataGridView_Trips_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
rowIndexFromMouseDown = dataGridView_Trips.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 dataGridView_Routes_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView_Routes_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataRowView)))
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = dataGridView_Routes.PointToClient(new Point(e.X, e.Y));
// If the drag operation was a copy then add the row to the other control.
if (e.Effect == DragDropEffects.Copy)
{
DataGridViewRow rowToMove = e.Data(typeof(DataGridViewRow)) as DataGridViewRow;
dataGridView_Routes.Rows.Add(rowToMove);
}
}
else
{
log("Geen data! #01", "Fout");
}
}
/* End Drag & Drop */
的數據類型不是「DataRowView的」。如果您有源代碼,請檢查'if(e.Data.GetDataPresent(typeof(DataRowView)))'並查看要刪除的數據的類型。例如:它可能是文本數據,在這種情況下,類型是System.String – prthrokz
這是對象包含的內容。 http://daven.nl/c/img/so-datagridviewrow.jpg這是一個datagridviewrow ... – Perfection
我很困惑,重建部分修復了應用程序。 – Perfection