【发布时间】:2011-08-29 21:04:43
【问题描述】:
我有一个带有行的网格视图。我希望用户能够抓取一行,并将其移动到 winform 上的其他几个网格视图之一。我该怎么做呢?我不熟悉这里如何实现拖放。
关于这种拖放的任何好的教程?感谢您的帮助。
更新:好的,我有以下代码(从 gridPODetails 拖动到 dataGridView1。它还没有工作,但我更近了(现在我在目的地得到拖动箭头和加号)。我错过了什么?
private void gridPODetails_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = gridPODetails.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
//DataRowView view = (DataRowView)gridPODetails.Rows[info.RowIndex].DataBoundItem; //WRONG
DataRow view = ((DataTable)(gridPODetails.DataSource)).Rows[info.RowIndex]; //RIGHT
if (view != null)
{
gridPODetails.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();
}
}
已解决:请参阅上面的编辑。我实际上是在抓取整个数据表,而不仅仅是我想要的行。当然,目的地只知道如何使用行,而不是整个表。现在它的工作!
【问题讨论】:
-
@Charles - 是的,我不确定这还有什么意义?
-
删除了我的答案。有问题的网格是 DevExpress 网格。它不是标准的 DataGridView。很抱歉浪费了您的时间。
-
@David Stratton - 我实际上发现其中一些有用(尽管不完整)。真正的 gridview 有一个 HitTest 功能可以做同样的事情。
-
@David Stratton -在你的链接和这个codeproject.com/KB/cpp/DataGridView_Drag-n-Drop.aspx之间我更近了。但仍然缺少一些东西。
-
@David Stratton - 将您的链接作为答案放回去,它对我有帮助,您应该得到答案。
标签: c# winforms gridview drag-and-drop