在DataGridView中拖放行 [英] Drag and Drop Row in a DataGridView

查看:169
本文介绍了在DataGridView中拖放行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DataGridView的窗体,我需要启用在这个DataGridView中拖放行。



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



这是 Load form DataGridView 从数据库中填写

  DataTable bsPeople ; 
矩形dragBoxFromMouseDown;
int rowIndexFromMouseDown;
int rowIndexOfItemUnderMouseToDrop;
private void Form1_Load(object sender,EventArgs e)
{
DataGridView1.AllowDrop = true;
bsPeople = objPeople.ReturnPeople(); //从SQL Server
填充数据DataGridView1.DataSource = bsPeople;
}

这是拖放事件

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

private void dataGridView1_MouseDown(object sender,MouseEventArgs e)
{
rowIndexFromMouseDown = DataGridView1.HitTest(eX, eY).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];
(int col = 0;(col
< =(rowToMove.Cells.Count - 1)); col ++)
{
celldata [col] = rowToMove.Cells [col]。
}

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

}
}
}


解决方案

拖曳删除



您需要删除并插入右侧 DataRows 。尝试这样:

  private void DGV_DragDropData(object sender,DragEventArgs e)
{
Point clientPoint = DGV .PointToClient(new Point(eX,eY));
rowIndexOfItemUnderMouseToDrop =
DGV.HitTest(clientPoint.X,clientPoint.Y).RowIndex;
if(e.Effect == DragDropEffects.Move)
{
DataGridViewRow rowToMove =(DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
//找到要在数据源中移动的行:
DataRow oldrow =((DataRowView)rowToMove.DataBoundItem).Row;
//克隆它:
DataRow new = bsPeople.NewRow();
newrow.ItemArray = oldrow.ItemArray;
bsPeople.Rows.InsertAt(newrow,rowIndexOfItemUnderMouseToDrop);
bsPeople.Rows.Remove(oldrow);
}
}

请注意,代码只能移动 / strong>行。为了移动多行,还需要一些更多的技巧。



行状态



另请注意,您可能需要根据需要设置新行的 RowState 。在添加的时候,你可能需要通过调用 newRow.AcceptChanges()或者可能对其他一些来设置它不变状态,取决于原始行的状态。



要修改 RowState 一些规则适用:




  • DataRow 必须驻留在 DataTable 中;刚创建完成后,状态为 Unattached ,您无法更改。

  • 只有不变的行可以通过 SetAdded SetModified 方法更改;所以你必须先调用 AcceptChanges()



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

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


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.

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

This are the Drag and Drop events

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 & Drop

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..

State of the Row:

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.

To modify the RowState a few rules apply:

  • 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.

This should do the job:

   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中拖放行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆