/// <summary> /// Change the position of a given row /// </summary> /// <param name="position">new position</param> /// <param name="dataRow">row to be moved</param> private void ChangePosition(int position, DataGridViewRow dataRow) { if (null == dataRow) { return; } CTCTestCaseDetailsDataSet.TestCaseDetailsDataTable dtable = cTCTestCaseDetails.TestCaseDetails; DataRow row = ((DataRowView)dataRow.DataBoundItem).Row; DataRow newRow = cTCTestCaseDetails.TestCaseDetails.NewTestCaseDetailsRow(); newRow.ItemArray = row.ItemArray; if (position == -1 || position == (testCaseDetails_DataGrid.Rows.Count - 1)) { position += 1; } dtable.Rows.InsertAt(newRow, position); testCaseDetails_DataGrid.Rows[position].Selected = true; testCaseDetails_DataGrid.CurrentCell = testCaseDetails_DataGrid.Rows[position].Cells[0]; testCaseDetails_DataGrid.DataBindingComplete -= testCaseDetails_DataGrid_DataBindingComplete; dtable.Rows.Remove(row); testCaseDetails_DataGrid.DataBindingComplete += testCaseDetails_DataGrid_DataBindingComplete; dtable.AcceptChanges(); }
/// <summary> /// Move the position of a given row /// </summary> /// <param name="direction">direction of movement</param> /// <param name="dataRow">row to be moved</param> private void MoveRow(DataGridViewRow dataRow, RowMovement direction) { // Perform no operation when no rows are selected if (null == dataRow) { return; } CTCTestCaseDetailsDataSet.TestCaseDetailsDataTable dtable = cTCTestCaseDetails.TestCaseDetails; DataRow currentRow = ((DataRowView)dataRow.DataBoundItem).Row; DataRow newRow = cTCTestCaseDetails.TestCaseDetails.NewTestCaseDetailsRow(); newRow.ItemArray = currentRow.ItemArray; int newRowIndex = 0; if (direction == RowMovement.MoveDown) { newRowIndex = (dataRow.Index + 2) > testCaseDetails_DataGrid.Rows.Count ? testCaseDetails_DataGrid.Rows.Count : (dataRow.Index + 2); } else if (direction == RowMovement.MoveUp) { newRowIndex = (dataRow.Index - 1) < 0 ? 0 : (dataRow.Index - 1); } else if (direction == RowMovement.MoveBottom) { newRowIndex = testCaseDetails_DataGrid.Rows.Count; } dtable.Rows.InsertAt(newRow, newRowIndex); testCaseDetails_DataGrid.Rows[newRowIndex].Selected = true; testCaseDetails_DataGrid.CurrentCell = testCaseDetails_DataGrid.Rows[newRowIndex].Cells[0]; testCaseDetails_DataGrid.DataBindingComplete -= testCaseDetails_DataGrid_DataBindingComplete; dtable.Rows.Remove(currentRow); testCaseDetails_DataGrid.DataBindingComplete += testCaseDetails_DataGrid_DataBindingComplete; dtable.AcceptChanges(); }