Le Drag & Drop entre DataGridView

J'ai copié un peu de code et l'a modifié pour l'adapter à ma demande. Et je vais continuer de peaufiner et de nettoyer le code jusqu'à ce que je suis statisfied avec elle. Mais j'ai rencontré un peu d'erreur. J'ai deux datagridviews et souhaitez déplacer datagridrows de l'un à l'autre. Cependant, alors que la drag&drop des événements toute l'incendie, le dataGridView_Routes_DragDrop() va exécuter la commande log car il n'y a pas de données dans e.Data.GetData. Qu'ai-je fait de mal? Ai-je raté quelque chose? J'ai essayé de regarder à travers plusieurs guides, mais rien ne porte expressément sur cette question.

Comment puis-je obtenir la grille de passer le traîné datagridrow de l'autre datagrid?

    /* Drag & Drop */
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private void dataGridView_Trips_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
//If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
//Proceed with the drag and drop, passing in the list item.                    
DragDropEffects dropEffect = dataGridView_Trips.DoDragDrop(dataGridView_Trips.Rows[rowIndexFromMouseDown], DragDropEffects.Copy);
}
}
}
private void dataGridView_Trips_MouseDown(object sender, MouseEventArgs e)
{
//Get the index of the item the mouse is below.
rowIndexFromMouseDown = dataGridView_Trips.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
//Remember the point where the mouse down occurred. 
//The DragSize indicates the size that the mouse can move 
//before a drag event should be started.                
Size dragSize = SystemInformation.DragSize;
//Create a rectangle using the DragSize, with the mouse position being
//at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
else
//Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dataGridView_Routes_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView_Routes_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataRowView)))
{
//The mouse locations are relative to the screen, so they must be 
//converted to client coordinates.
Point clientPoint = dataGridView_Routes.PointToClient(new Point(e.X, e.Y));
//If the drag operation was a copy then add the row to the other control.
if (e.Effect == DragDropEffects.Copy)
{
DataGridViewRow rowToMove = e.Data(typeof(DataGridViewRow)) as DataGridViewRow;
dataGridView_Routes.Rows.Add(rowToMove);
}
}
else
{
log("Geen data! #01", "Fout");
}
}
/* End Drag & Drop */
  • Les données que vous essayez de tomber n'est pas de type 'DataRowView'. Si vous avez la source, l'inspection de la " si(e.Les données.GetDataPresent(typeof(DataRowView))) " et voir quel est le type de données supprimées. Par exemple: Il peut s'agir de textes de données dans laquelle les cas, le type de Système.Chaîne
  • C'est ce que l'objet contient. daven.nl/c/img/so-datagridviewrow.jpg C'est un datagridviewrow...
  • Je suis tellement confus, la reconstruction de la correction de l'application partiellement.
  • Lié à: stackoverflow.com/questions/1620947/...
InformationsquelleAutor Perfection | 2013-01-07