我想出了如何在how to drag gridview row from one grid to another的datagridviews之间拖动行,但现在我遇到了问题。我可以将行从gridPODetails拖到DataGridView1。我可以将行从DataGridView1拖回gridPODetails。但之后我一无所获。我希望能够无限地来回拖动,但我只能去那里。可能导致这种情况的原因以及如何纠正?
private void gridPODetails_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = gridPODetails.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
DataRow view = ((DataTable)(gridPODetails.DataSource)).Rows[info.RowIndex];
if (view != null)
{
gridPODetails.DoDragDrop(view, DragDropEffects.Copy);
}
}
}
private void gridPODetails_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void gridPODetails_DragDrop(object sender, DragEventArgs e)
{
DataGridView grid = sender as DataGridView;
DataTable table = grid.DataSource as DataTable;
DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
if (row != null && table != null && row.Table != table)
{
table.ImportRow(row);
row.Delete();
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
DataRow view = ((DataTable)(dataGridView1.DataSource)).Rows[info.RowIndex];
if (view != null)
{
dataGridView1.DoDragDrop(view, DragDropEffects.Copy);
}
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
DataGridView grid = sender as DataGridView;
DataTable table = grid.DataSource as DataTable;
DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
if (row != null && table != null && row.Table != table)
{
table.ImportRow(row);
row.Delete();
}
}
答案 0 :(得分:2)
在table.AcceptChanges()
之后添加row.Delete()
应允许您在表之间向后移动行。
原因可能是因为导入之前删除的行可能会导致问题。
答案 1 :(得分:2)
在回答MAW74656关于问题的评论时,如果我打算连接多个网格进行拖放操作,我将把这个方法放在一起。
本质上,我尝试创建一个lambda来将所有这些功能组合在一个方法中 - 如果需要允许多个调用者,它可以像它自己的方法一样完成。
这是:
Func<DataGridView, IEnumerable<Action>> configureDragDrop = grid =>
{
var dataTable = grid.DataSource as DataTable;
/* Event handler definitions here - see below */
grid.MouseDown += mds;
grid.DragEnter += des;
grid.DragDrop += dds;
return new Action[]
{
() => grid.MouseDown -= mds,
() => grid.DragEnter -= des,
() => grid.DragDrop -= dds,
};
};
这段代码允许我写这个:
// form-level field
private List<Action> removeHandlers = new List<Action>();
// in the method where `configureDragDrop` is defined
removeHandlers.AddRange(configureDragDrop(gridPODetails));
removeHandlers.AddRange(configureDragDrop(dataGridView1));
removeHandlers.AddRange(configureDragDrop(dataGridView2));
removeHandlers.AddRange(configureDragDrop(dataGridView3));
removeHandlers.AddRange(configureDragDrop(dataGridView4));
removeHandlers.AddRange(configureDragDrop(dataGridView5));
removeHandlers.AddRange(configureDragDrop(dataGridView6));
removeHandlers.AddRange(configureDragDrop(dataGridView7));
// etc
当我关闭我的表单时,我可以在一行中删除所有处理程序:
Array.ForEach(removeHandlers.ToArray(), rh => rh.Invoke());
事件处理程序看起来与原始代码非常相似 - 刚才是lambda格式。
MouseDown
:
MouseEventHandler mds = (smd, emd) =>
{
var info = grid.HitTest(emd.X, emd.Y);
if (info.RowIndex >= 0)
{
var dr = dataTable.Rows[info.RowIndex];
if (dr != null)
{
grid.DoDragDrop(dr, DragDropEffects.Copy);
}
}
};
DragEnter
:
DragEventHandler des = (sde, ede) =>
{
ede.Effect = DragDropEffects.Copy;
};
DragDrop
:
DragEventHandler dds = (sdd, edd) =>
{
var dr = edd.Data.GetData(typeof(DataRow)) as DataRow;
if (dr != null)
{
var dst = dataTable;
var src = dr.Table;
if (dst != src)
{
dst.ImportRow(dr);
dr.Delete();
src.AcceptChanges();
dst.AcceptChanges();
}
}
};
我希望这会有所帮助。