我想通了如何从how to drag gridview row from one grid to another datagridviews之间拖行,但现在我有一个问题。我可以将该行从gridPODetails拖到DataGridView1。我可以将行拖回到DataGridView1的gridPODetails。但在此之后,我什么也得不到。我希望能够无限地来回拖曳,但我只能往回走。什么可能导致这种情况以及如何纠正?拖放不工作在C#
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();
}
}
只是一个猜测,但也许'table.AcceptChanges'可能有帮助的事情 –
@Conrad Frix -where? – MAW74656
'row.Delete()' –