private void dataGrid_DragDrop(object sender, DragEventArgs e) { // The mouse locations are relative to the screen, so they must be // converted to client coordinates. Point clientPoint = dataGrid.PointToClient(new Point(e.X, e.Y)); // Get the row index of the item the mouse is below. rowIndexOfItemUnderMouseToDrop = dataGrid.HitTest(clientPoint.X, clientPoint.Y).RowIndex; // If the drag operation was a move then remove and insert the row. if (e.Effect == DragDropEffects.Move) { MySQL MySQLHandle = new MySQL(GlobalVar.sqlhost, GlobalVar.sqlport, GlobalVar.sqldatabase, GlobalVar.sqlusername, ""); MySqlConnection sqlReader = MySQLHandle.Connect(); DataGridViewRow rowToMove = e.Data.GetData( typeof(DataGridViewRow)) as DataGridViewRow; if(dataGrid.Rows[rowIndexOfItemUnderMouseToDrop].IsNewRow==false) { dataGrid.Rows.RemoveAt(rowIndexFromMouseDown); dataGrid.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove); for (int rowCounter = 0; rowCounter < dataGrid.RowCount; rowCounter++) { MySQLHandle.updateDriverLogOrder(dataGrid.Rows[rowCounter], sqlReader); } } for (int rowNumber = 0; rowNumber < dataGrid.Rows.Count; rowNumber++) { dataGrid.Rows[rowNumber].HeaderCell.Value = (rowNumber + 1).ToString(); } sqlReader.Close(); MySQLHandle.Disconnect(); } }