在 DataGridView 中拖放行

Drag and Drop Row in a DataGridView(在 DataGridView 中拖放行)
本文介绍了在 DataGridView 中拖放行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有带有 DataGridView 的 windows 窗体,我需要在此 DataGridView 中启用拖放行.

I have windows form with DataGridView and I need to enable drag and drop rows in this DataGridView.

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

It is filled from the database. My code does not work properly because after the first drag and drop I cant drop row to the right position.

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

This is the Load forms where DataGridView is filled from database

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);

        }
    }
}

推荐答案

Drag &掉落

您需要删除并插入right DataRows.试试这个:

You need to delete and insert the right DataRows. Try this:

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);
   }
}

请注意,代码仅移动 一个 行.要移动多行,还需要一些技巧..

Note that the code only moves one row. For moving multiple rows there are a few more tricks needed..

行状态:

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

Also note that you may need to set the RowState of the new row, depending on your needs. As it is added you may want to set it it Unchanged by calling newRow.AcceptChanges() or maybe to some other state, depending on the state of the original row.

要修改 RowState,需要遵循一些规则:

To modify the RowState a few rules apply:

  • DataRow 必须驻留在 DataTable 中;刚创建后,状态为Unattached,无法更改.
  • SetAddedSetModified 方法只能更改 Unchanged 行;所以你必须先调用 AcceptChanges().
  • The DataRow must reside in a DataTable; after you have just created it, the state is Unattached and you can't change it.
  • Only Unchanged rows can be changed by the SetAdded or SetModified methods; so you must call AcceptChanges() first.

这应该可以完成工作:

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

这些行必须在新行添加到表之后添加..

These lines must be added after the new row is added to the table..

这篇关于在 DataGridView 中拖放行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)