简体   繁体   English

如何将gridview行从一个网格拖到另一个

[英]how to drag gridview row from one grid to another

I have a gridview with rows. 我有一个带有行的gridview。 I'd like the user to be able to grab a row, and move it to one of several other gridviews on the winform. 我希望用户能够抓住一行并将其移到Winform上其他几个gridview之一。 How can I go about doing this? 我该怎么做呢? I'm not familiar with how to implement drag and drop here. 我不熟悉如何在此处实现拖放。

Any good tutorials on this sort of drag and drop? 关于这种拖放的任何好的教程? Thanks for your help. 谢谢你的帮助。

UPDATE: Ok, I have the following code (to drag from gridPODetails to dataGridView1. Its not working yet, but I"m closer (now I get drag arrow and plus sign in the destination). What am I missing? 更新:好的,我有以下代码(要从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();
    }
}

SOLVED: See my edit above. 已解决:请参见上面的编辑。 I was actually grabbing the entire datatable, not just the row I wanted. 我实际上是在抓取整个数据表,而不仅仅是我想要的行。 Of course the destination only knows how to work with Rows, not entire tables. 当然,目的地仅知道如何使用行,而不是整个表。 Now its working! 现在工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM