Reordering rows in DataGrid cause wrong value selected in Comb

Reordering rows in DataGrid cause wrong value selected in Comb

Post by UGF1bCB » Sat, 08 Dec 2007 21:20:00


Hi Linda
That works.
Just to point out what did it, compared to the FAQ, where I got the original
solution from - the difference is:

dataGridView1.Rows.Remove(gridRowToMove); // works
//dataGridView1.Rows.RemoveAt(rowIndexOfItemUnderMouseToDrop); does not work.

When I started moving the rows around I had errors with an uncommitted row.
This happened when I moved the last row or when I moved a row below the last
one.
I my case I could solve this by:

this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;

The root of the problem was the last un-commited row added by the grid when
the user was allowed to add rows.

Thanks again Linda.
--
Paul S
 
 
 

Reordering rows in DataGrid cause wrong value selected in Comb

Post by v-lli » Tue, 11 Dec 2007 11:56:45

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

 
 
 

Reordering rows in DataGrid cause wrong value selected in Comb

Post by UGF1bCB » Tue, 11 Dec 2007 16:17:03


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


--
Thanks a lot again Linda.
Paul S