在DataGridView中拖放行

时间:2016-01-16 09:14:16

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

我有使用DataGridView的Windows窗体,我需要在此DataGridView中启用拖放行。

从数据库填充。我的代码无法正常工作,因为在第一次拖放后我无法将行拖放到正确的位置。

这是Load表单,其中DataGridView从数据库中填充

DataTable bsPeople;
Rectangle dragBoxFromMouseDown;
    int rowIndexFromMouseDown;
    int rowIndexOfItemUnderMouseToDrop; 
    private void Form1_Load(object sender, EventArgs e)
    {
        DataGridView1.AllowDrop = true;
        bsPeople= objPeople.ReturnPeople(); // fill data from SQL Server 
        DataGridView1.DataSource = bsPeople;
    }

这是拖放事件

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (((e.Button ==MouseButtons.Left)))
        {
            if (((dragBoxFromMouseDown != Rectangle.Empty)
                        && !dragBoxFromMouseDown.Contains(e.X, e.Y)))
            {
                DragDropEffects dropEffect = DataGridView1.DoDragDrop(DataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
            }
        }
    }

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        rowIndexFromMouseDown = DataGridView1.HitTest(e.X, e.Y).RowIndex;
        if ((rowIndexFromMouseDown != -1))
        {
            Size dragSize = SystemInformation.DragSize;
            dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
        }
        else
        {
            dragBoxFromMouseDown = Rectangle.Empty;
        }
    }

    private void dataGridView1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        Point clientPoint = DataGridView1.PointToClient(new Point(e.X, e.Y));
        rowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
        if ((e.Effect == DragDropEffects.Move))
        {
            DataGridViewRow rowToMove = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
            object[] celldata=new object[DataGridView1.ColumnCount];
            for (int col = 0; (col
                        <= (rowToMove.Cells.Count - 1)); col++)
            {
                celldata[col] = rowToMove.Cells[col].Value;
            }

            DataRow row = bsPeople.NewRow();
            row.ItemArray = celldata;
            bsPeople.Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop);
            rowToMove.DataGridView.Rows.Remove(rowToMove);

        }
    }
}

1 个答案:

答案 0 :(得分:1)

拖动&amp;删除

您需要删除并插入正确 DataRows。试试这个:

private void DGV_DragDropData(object sender, DragEventArgs e)
{
   Point clientPoint = DGV.PointToClient(new Point(e.X, e.Y));
   rowIndexOfItemUnderMouseToDrop =
       DGV.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
   if (e.Effect == DragDropEffects.Move)
   {
      DataGridViewRow rowToMove = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
      // find the row to move in the datasource:
      DataRow oldrow = ((DataRowView)rowToMove.DataBoundItem).Row;
      // clone it:
      DataRow newrow = bsPeople.NewRow();
      newrow.ItemArray = oldrow.ItemArray;
      bsPeople.Rows.InsertAt(newrow, rowIndexOfItemUnderMouseToDrop);
      bsPeople.Rows.Remove(oldrow);
   }
}

请注意,代码只移动一个行。对于移动多行,需要更多技巧..

行的状态:

另请注意,您可能需要根据需要设置新行的RowState。在添加时,您可能希望通过调用Unchanged或可能将其设置为其他状态newRow.AcceptChanges(),具体取决于原始行的状态。

修改RowState适用的一些规则:

  • DataRow 必须驻留在DataTable;在您刚刚创建它之后,状态为Unattached,您无法更改它。
  • UnchangedSetAdded方法只能更改SetModified行;所以你必须先致电AcceptChanges()

这应该做的工作:

   if (oldrow.RowState != DataRowState.Unchanged) newrow.AcceptChanges();
   if (oldrow.RowState == DataRowState.Added) newrow.SetAdded();
   if (oldrow.RowState == DataRowState.Modified) newrow.SetModified();

必须在新行添加到表格后添加这些行。