/// <summary> /// User has hit move up. Move contents of selected cells up one row. /// </summary> private void OnMoveUp(object sender, EventArgs e) { try { // Save the current selection SaveSelection(); // Work out the dimensions of the current selection. int StartRow, StartCol, EndRow, EndCol; CalcStartEndColAndRow(out StartRow, out StartCol, out EndRow, out EndCol); if (StartRow > 0) { // Get the contents of the current selection string ContentsToMoveUp = GetClipboardContent().GetText(); // no readonly cells now in source selection or destination for (int row = StartRow - 1; row <= EndRow; row++) { for (int Col = StartCol; Col <= EndCol; Col++) { Rows[row].Cells[Col].ReadOnly = false; } } // Get the contents of the row above the current selection. ClearSelection(); for (int Col = StartCol; Col <= EndCol; Col++) { Rows[StartRow - 1].Cells[Col].Selected = true; } string ContentsToPutInEndRow = GetClipboardContent().GetText(); // Paste the contents into the row below. List <string> ColumnsChanged = Paste(ContentsToMoveUp, this.Rows[StartRow - 1].Cells[StartCol], false); // Paste the bit we replaced into the end row. Paste(ContentsToPutInEndRow, this.Rows[EndRow].Cells[StartCol], false); // Now invoke the table column changed event. if (TableColumnChangedEvent != null) { TableColumnChangedEvent.Invoke(ColumnsChanged); } // Restore the selection but up a row. RestoreSelection(-1); } } catch (Exception err) { MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// We need to trap the end of an edit and write the data back to the /// table. /// </summary> protected override void OnCellValueChanged(DataGridViewCellEventArgs e) { base.OnCellValueChanged(e); if (DataSourceTable != null && !InRefresh && e.RowIndex != -1 && e.ColumnIndex != -1) { try { int Col = e.ColumnIndex; int Row = e.RowIndex; if (Row > DataSourceTable.Rows.Count - 1) { DataSourceTable.Rows.Add(DataSourceTable.NewRow()); } // Make sure this row has our popup menu. if (Rows[e.RowIndex].ContextMenuStrip == null) { Rows[e.RowIndex].ContextMenuStrip = PopupMenu; } ContextMenuStrip = PopupMenu; if (DataSourceTable.Columns[Col].DataType == typeof(double) && !MathUtility.IsNumerical(Rows[Row].Cells[Col].Value.ToString())) { // Turn the column into a string column. // Capture state of existing column and remove it from DataTable. string ColumnName = DataSourceTable.Columns[Col].ColumnName; string[] Values = GridUtility.GetColumnAsStringsUsingCellFormat(this, Col); int Ordinal = DataSourceTable.Columns[Col].Ordinal; DataSourceTable.Columns.RemoveAt(Col); // Create a new column of string type. DataColumn NewColumn = DataSourceTable.Columns.Add(ColumnName, typeof(string)); NewColumn.SetOrdinal(Ordinal); DataTableUtility.AddColumn(DataSourceTable, ColumnName, Values); } DataSourceTable.Rows[Row][Col] = Rows[Row].Cells[Col].Value; ChangedColumnNames.Add(DataSourceTable.Columns[Col].ColumnName); if (TableColumnChangedEvent != null) { TableColumnChangedEvent.Invoke(ChangedColumnNames); } ChangedColumnNames.Clear(); } catch (Exception) { } // This can happen when the user puts a text value into a numeric column. } }
/// <summary> /// User has hit paste. Paste the contents of the clipboard into the grid. /// </summary> private void OnPaste(object sender, EventArgs e) { try { List <string> ColumnsChanged = Paste(Clipboard.GetText(), CurrentCell, true); if (ColumnsChanged.Count > 0 && TableColumnChangedEvent != null) { TableColumnChangedEvent.Invoke(ColumnsChanged); } } catch (Exception err) { MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// User has hit delete. Clear contents of selected cells. /// </summary> private void OnDelete(object sender, EventArgs e) { try { if (!IsCurrentCellInEditMode) { InRefresh = true; List <string> ColumnsChanged = new List <string>(); SaveSelection(); // Make the values in the grid & table null. foreach (Point Cell in SavedSelections) { int Col = Cell.X; int Row = Cell.Y; if (Row < Rows.Count - 1) { Rows[Row].Cells[Col].Value = null; if (DataSourceTable != null) { DataSourceTable.Rows[Row][Col] = Rows[Row].Cells[Col].Value; } if (!ColumnsChanged.Contains(Columns[Col].HeaderText)) { ColumnsChanged.Add(Columns[Col].HeaderText); } } } InRefresh = false; // Now invoke the table column changed event. if (TableColumnChangedEvent != null) { TableColumnChangedEvent.Invoke(ColumnsChanged); } } } catch (Exception err) { MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }