【问题标题】:how to drag gridview row from one grid to another如何将gridview行从一个网格拖到另一个网格
【发布时间】: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


【解决方案1】:

我在Bing search 上找到了这个。它看起来正是您正在寻找的东西。

http://tv.devexpress.com/Content/XtraGrid/XtraGridDragRowsBetweenGrids/XtraGridDragRowsBetweenGrids.pdf

【讨论】:

  • -这看起来很完美,我将尝试使用并告诉你它是如何使用的。您认为该视频无法在线观看?
  • 我不这么认为,但我发现印刷的文章很容易理解。我必须感谢你的提问。自从我试图弄清楚如何做到这一点以来已经有很长时间了,我放弃了它,因为不值得努力。 (那时我是一个相对较新的开发人员。)现在我找到了这篇文章,它看起来比我学习 .NET 1.0 时要容易得多。
  • -这段代码是指内置的.NET gridview,还是来自devexpress的自定义控件?
  • -我无法声明 GridHitInfo 对象。我必须添加一些奇怪的命名空间吗?
  • 该死。这是一个 DevExpress 网格——我的回答对你来说毫无价值。该视频是可用的,但现在这是一个有争议的问题。对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 2019-03-29
  • 2022-06-18
相关资源
最近更新 更多