我已经复制了一些代码并对其进行了修改以适合我的应用程序。并且我将继续调整和清理代码,直到对其进行统计。但是我遇到了一个小错误。我有两个datagridviews,希望将datagridrows从一个移到另一个。但是,当drag&drop
事件全部触发时,由于dataGridView_Routes_DragDrop()
中没有数据,所以e.Data.GetData
将执行log命令。我做错了什么?我想念什么吗?我尝试浏览了几本指南,但没有任何内容专门涵盖此问题。
如何使数据网格将拖动的数据网格传递到其他数据网格?
/* 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 */
最佳答案
此解决方案适用于将datagridViews绑定到customObjects的人员。它可以像多种选择一样吸引人。建议被接受。
假设您要从datagridview1拖到datagridview2
//datagridview1 is bound to this BindingList
BindingList<myObject> object_bound_list1;
//datagridview2 is bound to this BindingList
BindingList<myObject> object_bound_list2;
List<myObject> selected_Object_list = new List<myObject>();
List<int> selected_pos_list = new List<int>();
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(
selected_Object_list,
DragDropEffects.Move);
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
int rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
//if shift key is not pressed
if (Control.ModifierKeys != Keys.Shift && Control.ModifierKeys != Keys.Control)
{
//if row under the mouse is not selected
if (!selected_pos_list.Contains(rowIndexFromMouseDown) && rowIndexFromMouseDown > 0)
{
//if there only one row selected
if (dataGridView1.SelectedRows.Count == 1)
{
//select the row below the mouse
dataGridView.ClearSelection();
dataGridView1.Rows[rowIndexFromMouseDown].Selected = true;
}
}
}
//clear the selection lists
selected_Object_list.Clear();
selected_pos_list.Clear();
//add the selected objects
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
selected_Object_list.Add(object_bound_list1[row.Index]);
selected_pos_list.Add(row.Index);
}
}
private void dataGridView2_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
if (e.Effect == DragDropEffects.Move)
{
foreach (var item in selected_Object_list)
{
object_bound_list2.Add(item);
}
}
}
关于c# - 在DataGridView之间拖放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14199510/