Hi Paul,
Thank you for your reply!
> The root of the problem was the last un-commited row added by the grid
when the user was allowed to add rows.
Yes, you're right. We cannot remove the new row or insert a DataGridViewRow
after the new row in a DataGridView. As you said, the solution is to set
the AllowUserToAddRows property of the DataGridView to true.
> I works as I wrote in my last mail, but when the control is databound I
get the error:
No, we cannot add or remove a DataGridViewRow programmtically to a
DataGridView when this DataGridView is data-bound. To do this, you may
exchange the rows in the underlying data source instead. The following is a
sample:
DataRow row = dataTable1.Rows[0];
DataRowState rowstate = row.RowState;
object[] itemarray = row.ItemArray;
dataTable1.Rows.Remove(row);
row.ItemArray = itemarray;
this.LookupTable.Rows.InsertAt(row, 1);
if (rowstate == DataRowState.Added)
{
row.SetAdded();
}
else if (rowstate == DataRowState.Modified)
{
row.SetModified();
}
else if (rowstate == DataRowState.Unchanged)
{
row.AcceptChanges();
}
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Hi Paul,
Sorry that I made a mistake in my previous reply.
The line of code
this.LookupTable.Rows.InsertAt(row, 1);
should be
dataTable1.Rows.InsertAt(row, 1);
Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 10 Dec 2007 07:17 GMT
Hi Linda
At first I couldn't get it to work - then I deleted the part I didn't
understand - the RowState stuff - now it seems to work and it looks like this:
private void dgvLookup_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must
be
// converted to client coordinates.
Point clientPoint = this.dgvLookup.PointToClient(new Point(e.X,
e.Y));
// Get the row index of the item the mouse is below.
rowIndexOfItemUnderMouseToDrop =
this.dgvLookup.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 gridRow =
e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
DataRow rowToMove = dataTable1.Rows[gridRow.Index];
object[] itemarray = rowToMove.ItemArray;
dataTable1.Rows.Remove(rowToMove);
rowToMove.ItemArray = itemarray;
dataTable1.Rows.InsertAt(rowToMove,
rowIndexOfItemUnderMouseToDrop);
}
}

Signature
Thanks a lot again Linda.
Paul S