=====Drag & Drop===== /// /// Variables pour le D&D. /// private Rectangle dragBoxFromMouseDown; private int rowIndexFromMouseDown; private int rowIndexOfItemUnderMouseToDrop; #region IhmSynoptique D&D du DataGridView /// /// Drag & Drop : début de l'opération. Quand l'utilisateur enfonce le clic de la souris. /// /// On s'en fout. /// Information de l'événement. 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); } } /// /// 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. /// /// On s'en fout. /// On s'en fout. private void dataGridView1_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } /// /// Drag & Drop : vers la fin de l'opération. Quand l'utilisateur relâche le clic de la souris. /// /// On s'en fout. /// Position de la souris. 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; } } /// /// Drag & Drop : Fin de l'opération. Quand le logiciel applique réellement les modifications. /// /// On s'en fout. /// Informations diverses du D&D. 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 [[https://social.msdn.microsoft.com/Forums/windows/en-US/16b0a44e-35a0-4bc8-9ccd-ec2c62c95a55/select-and-drag-a-datagridview-row-with-a-single-click?forum=winforms|Select and drag a datagridview row with a single click]] {{ :lang:csharp:ihm:form:datagridview:select_and_drag_a_datagridview_row_with_a_single_click_2020-04-28_10_55_23_pm_.html |Archive du 17/06/2009 le 28/04/2020}} =====KeyDown, KeyUp, KeyPress===== Ils ne marchent pas. Voir [[:lang:csharp:ihm:form:touches#datagridview|Touches]]. =====Mettre en forme les cellules===== Il faut passer par l'évènement ''DataGridView.CellFormatting''. [[https://stackoverflow.com/questions/8063999/datagridview-cell-style-update|Datagridview cell style update]] {{ :lang:csharp:ihm:form:datagridview:c_-_datagridview_cell_style_update_-_stack_overflow_2020-04-28_10_55_22_pm_.html |Archive du 09/11/2011 le 28/04/2020}}