【问题标题】:How could I Drag and Drop DataGridView Rows under each other?如何将 DataGridView 行拖放到彼此下方?
【发布时间】:2010-12-09 21:47:18
【问题描述】:

我已经绑定了List<myClass>DataGridView,并按“myClass”中的“Priority”属性对其进行排序。 所以我想把一个“DataGridViewRow”拖到某个位置来改变它的“Priority”属性。

我如何“拖放”DataGridView 行?。 以及如何处理?

【问题讨论】:

    标签: c# .net datagridview drag-and-drop


    【解决方案1】:

    我在 MSDN 上找到了这个 code sample

    注意以下几点:

    1)。 DataGridView 属性 AllowDrop 必须设置为 true(默认为 false)。

    2)。当 DataGridView NOT 数据绑定时,下面的示例开箱即用。否则会抛出 InvalidOperationException。如果是数据绑定的,你应该操作DataSource中的项目顺序。

    private Rectangle dragBoxFromMouseDown;
    private int rowIndexFromMouseDown;
    private int rowIndexOfItemUnderMouseToDrop;
    private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // If the mouse moves outside the rectangle, start the drag.
            if (dragBoxFromMouseDown != Rectangle.Empty &&
                !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {
    
                // Proceed with the drag and drop, passing in the list item.                    
                DragDropEffects dropEffect = dataGridView1.DoDragDrop(
                dataGridView1.Rows[rowIndexFromMouseDown],
                DragDropEffects.Move);
            }
        }
    }
    
    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        // Get the index of the item the mouse is below.
        rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
    if (rowIndexFromMouseDown != -1)
        {
            // Remember the point where the mouse down occurred. 
         // The DragSize indicates the size that the mouse can move 
         // before a drag event should be started.                
            Size dragSize = SystemInformation.DragSize;
    
            // Create a rectangle using the DragSize, with the mouse position being
            // at the center of the rectangle.
            dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                                                           e.Y - (dragSize.Height / 2)),
                                dragSize);
        }
        else
            // Reset the rectangle if the mouse is not over an item in the ListBox.
            dragBoxFromMouseDown = Rectangle.Empty;
    }
    
    private void dataGridView1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }
    
    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        // The mouse locations are relative to the screen, so they must be 
        // converted to client coordinates.
        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
    
        // Get the row index of the item the mouse is below. 
        rowIndexOfItemUnderMouseToDrop =
            dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
    
        // If the drag operation was a move then remove and insert the row.
        if (e.Effect== DragDropEffects.Move)
        {
            DataGridViewRow rowToMove = e.Data.GetData(
                typeof(DataGridViewRow)) as DataGridViewRow;
            dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
            dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
    
        }
    }
    

    【讨论】:

    • 原文章的链接将不胜感激 :) 谢谢
    • 其实是两年前的,我想是这里的:social.msdn.microsoft.com/Forums/en-US/winforms/thread/…
    • 这个很棒的代码 sn-p 的一个小问题:如果您的 DGV 允许添加行,并且您将一行一直拖到最后,它会删除该行,然后尝试插入它通过了不允许的“虚拟”行,最终结果是删除的行将被删除(显然不是你想要的)。必须检查: this.uxSigningParties.Rows.RemoveAt(rowIndexFromMouseDown); if (rowIndexOfItemUnderMouseToDrop >= this.uxSigningParties.Rows.Count) { rowIndexOfItemUnderMouseToDrop--; }
    • idk 为什么我对这段代码有这样的问题,但似乎无论我把它放在哪里,它都会删除最后一个条目。
    【解决方案2】:

    “Wahid Bitar”的回答对我帮助很大。我无法评论以上内容,所以补充一点: 如果 'neminem' 评论说不需要删除一行:只需在最终移动它的行之前添加到 DragDrop 事件:

            if (rowIndexOfItemUnderMouseToDrop < 0 )
            {
                return;
            }        
           dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
           dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 2023-02-07
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多