lang:csharp:ihm:form:datagridview
Table des matières
Drag & Drop
/// <summary> /// Variables pour le D&D. /// </summary> private Rectangle dragBoxFromMouseDown; private int rowIndexFromMouseDown; private int rowIndexOfItemUnderMouseToDrop; #region IhmSynoptique D&D du DataGridView /// <summary> /// Drag & Drop : début de l'opération. Quand l'utilisateur enfonce le clic de la souris. /// </summary> /// <param name="sender">On s'en fout.</param> /// <param name="e">Information de l'événement.</param> private void dataGridView1_MouseMove(object sender, MouseEventArgs e) { if (((e.Button & MouseButtons.Left) == MouseButtons.Left) && (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))) { // Proceed with the drag and drop, passing in the list item. dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move); } } /// <summary> /// Drag & Drop : Un peu après le début de l'opération. Pour indiquer que le composant accepte le D&D en tant que destinataire. /// </summary> /// <param name="sender">On s'en fout.</param> /// <param name="e">On s'en fout.</param> private void dataGridView1_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } /// <summary> /// Drag & Drop : vers la fin de l'opération. Quand l'utilisateur relâche le clic de la souris. /// </summary> /// <param name="sender">On s'en fout.</param> /// <param name="e">Position de la souris.</param> private void dataGridView1_MouseDown(object sender, MouseEventArgs e) { // Get the index of the item the mouse is below. rowIndexFromMouseDown = dataGridView1.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; } } /// <summary> /// Drag & Drop : Fin de l'opération. Quand le logiciel applique réellement les modifications. /// </summary> /// <param name="sender">On s'en fout.</param> /// <param name="e">Informations diverses du D&D.</param> private void dataGridView1_DragDrop(object sender, DragEventArgs e) { // The mouse locations are relative to the screen, so they must be // converted to client coordinates. Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y)); // Get the row index of the item the mouse is below. // dataGridView1.HitTest peut valoir -1 si on lache sur l'entête de la colonne. rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex; // If the drag operation was a move then remove and insert the row. if ((e.Effect == DragDropEffects.Move) && (rowIndexOfItemUnderMouseToDrop != -1)) { DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow; // nouvelle position : rowIndexOfItemUnderMouseToDrop. // ancienne position : rowIndexFromMouseDown. // Appliquer un traitement ici. dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown); dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove); } } #endregion
Select and drag a datagridview row with a single click Archive du 17/06/2009 le 28/04/2020
KeyDown, KeyUp, KeyPress
Ils ne marchent pas. Voir Touches.
Mettre en forme les cellules
Il faut passer par l'évènement DataGridView.CellFormatting
.
Datagridview cell style update Archive du 09/11/2011 le 28/04/2020
lang/csharp/ihm/form/datagridview.txt · Dernière modification : 2020/04/28 22:57 de root