问题描述
我有带有 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
,无法更改.SetAdded
或SetModified
方法只能更改Unchanged
行;所以你必须先调用AcceptChanges()
.
- The
DataRow
must reside in aDataTable
; after you have just created it, the state isUnattached
and you can't change it. - Only
Unchanged
rows can be changed by theSetAdded
orSetModified
methods; so you must callAcceptChanges()
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 中拖放行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!