public bool ProcessDGVEditShortcutKeys(DataGridView dgv, KeyEventArgs e, string cno, LookupTables lookupTables, AppSettings appSettings) { bool keyProcessed = false; // Change cursor to the wait cursor... Cursor origCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; if (e.KeyCode == Keys.D && e.Control) { Dictionary<int, int> selectedColumnMinRow = new Dictionary<int, int>(); // Processing keystroke... keyProcessed = true; foreach (DataGridViewCell cell in dgv.SelectedCells) { // If the min selected row has not been found for this column - find it now... if (!selectedColumnMinRow.ContainsKey(cell.ColumnIndex)) { int minRow = dgv.Rows.Count; ; // Find the minimum row index in this column's selected cells... minRow = cell.RowIndex; for (int i = minRow; i > -1; i--) { if (dgv.SelectedCells.Contains(dgv[cell.ColumnIndex, i])) minRow = i; } // If the user is trying to perform a copy down (CTRL+D) using the row for adding a new row as the source row - bail out now... if (dgv.Rows[minRow].IsNewRow) return false; //Save the min row for this column in the dictionary selectedColumnMinRow.Add(cell.ColumnIndex, minRow); } // object newValue = ((DataRowView)dgv.Rows[selectedColumnMinRow[cell.ColumnIndex]].DataBoundItem)[cell.ColumnIndex]; DataRowView dr = (DataRowView)cell.OwningRow.DataBoundItem; if (dr == null) //if (dgv.Rows[row].IsNewRow) { // Couldn't find a bound row so this must be the 'new row' row in the datagrid... dgv[cell.ColumnIndex, cell.RowIndex].Value = newValue; dgv.UpdateCellValue(cell.ColumnIndex, cell.RowIndex); } else { if (!dr[cell.ColumnIndex].Equals(newValue)) { // Edit the DataRow (not the DataRowView) so that row state is changed... dr.Row[cell.ColumnIndex] = newValue; } } } } if (e.KeyCode == Keys.E && e.Control) { // Processing keystroke... keyProcessed = true; DataRow dr = ((DataRowView)dgv.CurrentCell.OwningRow.DataBoundItem).Row; string columnName = dgv.CurrentCell.OwningColumn.Name; if (dr.Table.Columns[columnName].ExtendedProperties.ContainsKey("gui_hint") && dr.Table.Columns[columnName].ExtendedProperties["gui_hint"].ToString().ToUpper() == "TEXT_CONTROL") { string currentCellValue = dr[columnName].ToString(); RichTextEditor rte = new RichTextEditor(currentCellValue, false); if (rte.ShowDialog() == DialogResult.OK) { dr[columnName] = rte.RichTextMessage; } } } if (e.KeyCode == Keys.N && e.Control) { if (dgv.CurrentRow != null && dgv.CurrentRow.Selected && !dgv.CurrentRow.IsNewRow) { DataTable dt = (DataTable)((BindingSource)dgv.DataSource).DataSource; DataRow sourceRow = null; DataRow destRow = null; // Processing keystroke... keyProcessed = true; if (dt != null) { sourceRow = dt.DefaultView[dgv.CurrentRow.Index].Row; destRow = dt.NewRow(); } if (sourceRow != null) { foreach (DataColumn dc in dt.Columns) { if (!dt.PrimaryKey.Contains(dc) && !dc.ReadOnly) { switch (dc.ColumnName) { case "created_by": case "owned_by": if (string.IsNullOrEmpty(cno)) { destRow[dc] = sourceRow[dc]; } else { destRow[dc] = cno; } break; case "created_date": case "owned_date": destRow[dc] = DateTime.Now; break; case "modified_by": case "modified_date": break; default: // Column is not a required field (or is a boolean field that only allows Y or N) destRow[dc] = sourceRow[dc]; break; } } } dt.Rows.InsertAt(destRow, dgv.CurrentRow.Index + 1); //RefreshDGVRowFormatting(dgv.Rows[dgv.CurrentRow.Index + 1], ux_checkboxHighlightChanges.Checked); } } } if (e.KeyCode == Keys.OemQuotes && e.Control) { if (dgv.CurrentRow != null && dgv.CurrentRow.Index > 0) { int sourceRowIndex; DataRow sourceRow; DataRow destinationRow; // Processing keystroke... keyProcessed = true; if (dgv.CurrentRow.IsNewRow) { dgv.BeginEdit(true); sourceRowIndex = dgv.CurrentRow.Index - 1; sourceRow = ((DataRowView)dgv.Rows[sourceRowIndex].DataBoundItem).Row; destinationRow = ((DataRowView)dgv.CurrentRow.DataBoundItem).Row; } else { sourceRowIndex = dgv.CurrentRow.Index - 1; sourceRow = ((DataRowView)dgv.Rows[sourceRowIndex].DataBoundItem).Row; destinationRow = ((DataRowView)dgv.CurrentRow.DataBoundItem).Row; } if (sourceRow != null && destinationRow != null) { if (!destinationRow[dgv.CurrentCell.ColumnIndex].Equals(sourceRow[dgv.CurrentCell.ColumnIndex])) { if (!dgv.Columns[dgv.CurrentCell.ColumnIndex].ReadOnly) { destinationRow[dgv.CurrentCell.ColumnIndex] = sourceRow[dgv.CurrentCell.ColumnIndex]; } } } //RefreshDGVRowFormatting(dgv.CurrentCell.OwningRow, ux_checkboxHighlightChanges.Checked); } } if (e.KeyCode == Keys.V && e.Control) { IDataObject dataObj = Clipboard.GetDataObject(); string pasteText = ""; //string[] junk = dataObj.GetFormats(); if (dataObj.GetDataPresent(System.Windows.Forms.DataFormats.UnicodeText)) { char[] rowDelimiters = new char[] { '\r', '\n' }; char[] columnDelimiters = new char[] { '\t' }; int badRows = 0; int missingRows = 0; bool importSuccess = false; // Processing keystroke... keyProcessed = true; pasteText = dataObj.GetData(DataFormats.UnicodeText).ToString(); DataTable dt = (DataTable)((BindingSource)dgv.DataSource).DataSource; importSuccess = ImportTextToDataTableUsingKeys(pasteText, dt, rowDelimiters, columnDelimiters, out badRows, out missingRows, lookupTables, appSettings); if (!importSuccess) { // Paste the text into the DGV in 'block style' importSuccess = ImportTextToDataTableUsingBlockStyle(pasteText, dgv, rowDelimiters, columnDelimiters, out badRows, out missingRows, lookupTables, appSettings); } //RefreshMainDGVFormatting(); //RefreshForm(); } } if (e.KeyCode == Keys.C && e.Control) { string copyString = ""; // First we need to get the min/max rows and columns for the selected cells... int minCol = dgv.Columns.Count; int maxCol = -1; int minRow = dgv.Rows.Count; int maxRow = -1; // Processing keystroke... keyProcessed = true; foreach (DataGridViewCell dgvc in dgv.SelectedCells) { if (dgvc.ColumnIndex < minCol) minCol = dgvc.ColumnIndex; if (dgvc.ColumnIndex > maxCol) maxCol = dgvc.ColumnIndex; if (dgvc.RowIndex < minRow) minRow = dgvc.RowIndex; if (dgvc.RowIndex > maxRow) maxRow = dgvc.RowIndex; } // First, gather the column headers (but only if the entire row was selected)... if (dgv.SelectedRows.Count != 0) { for (int i = minCol; i <= maxCol; i++) { copyString += dgv.Columns[i].HeaderText + '\t'; //copyString += dgv.Columns[i].Name + '\t'; } // Strip the last tab and insert a newline... copyString = copyString.TrimEnd('\t'); copyString += "\r\n"; } // Now build the string to pass to the clipboard... for (int i = minRow; i <= maxRow; i++) { for (int j = minCol; j <= maxCol; j++) { switch (dgv[j, i].FormattedValueType.Name) { case "Boolean": copyString += dgv[j, i].Value.ToString() + '\t'; break; default: if (dgv[j, i].FormattedValue == null || dgv[j, i].FormattedValue.ToString().ToLower() == "[null]") { copyString += "" + '\t'; } else { copyString += dgv[j, i].FormattedValue.ToString() + '\t'; } break; } } copyString = copyString.TrimEnd('\t'); copyString += "\r\n"; } copyString = copyString.TrimEnd('\n'); copyString = copyString.TrimEnd('\r'); // Pass the new string to the clipboard... Clipboard.SetDataObject(copyString, false, 1, 1000); //RefreshMainDGVFormatting(); //RefreshForm(); } if (e.KeyCode == Keys.Delete) { // Processing keystroke... keyProcessed = true; if (dgv.SelectedRows.Count == 0) { // The user is deleting values from individual selected cells (not entire rows)... foreach (DataGridViewCell dgvc in dgv.SelectedCells) { DataRowView drv = (DataRowView)dgvc.OwningRow.DataBoundItem; if (drv == null) //if (dgv.Rows[row].IsNewRow) { dgvc.Value = ""; dgv.UpdateCellValue(dgvc.ColumnIndex, dgvc.RowIndex); //dgv[dgvc.ColumnIndex, dgvc.RowIndex].Style.BackColor = Color.Yellow; } else { if (!drv[dgvc.OwningColumn.Index].Equals(DBNull.Value)) { if (!dgvc.ReadOnly) { // Edit the DataRow (not the DataRowView) so that row state is changed... drv.Row[dgvc.OwningColumn.Index] = DBNull.Value; // For unbound text cells we have to manually clear the cell's text... if (string.IsNullOrEmpty(dgvc.OwningColumn.DataPropertyName)) dgvc.Value = ""; dgv.UpdateCellValue(dgvc.ColumnIndex, dgvc.RowIndex); //dgv[dgvc.ColumnIndex, dgvc.RowIndex].Style.BackColor = Color.Yellow; } } } //RefreshDGVRowFormatting(dgvc.OwningRow, ux_checkboxHighlightChanges.Checked); } } else { // The user is attempting to delete entire rows from the datagridview... //SharedUtils sharedUtils = new SharedUtils(lookupTables.WebServiceURL, lookupTables.Username, lookupTables.Password_ClearText, true); SharedUtils sharedUtils = new SharedUtils(_webServices.Url, _webServices.Username, _webServices.Password_ClearText, true, ""); GRINGlobal.Client.Common.GGMessageBox ggMessageBox = new GRINGlobal.Client.Common.GGMessageBox("WARNING!!! You are about to permanently delete {0} records from the central database!\n\nAre you sure you want to do this?", "Record Delete Confirmation", MessageBoxButtons.OKCancel, MessageBoxDefaultButton.Button2); ggMessageBox.Name = "UserInterfaceUtils_ProcessDGVEditShortcutKeysMessage1"; if (sharedUtils != null && sharedUtils.IsConnected) sharedUtils.UpdateControls(ggMessageBox.Controls, ggMessageBox.Name); //if (ggMessageBox.MessageText.Contains("{0}")) ggMessageBox.MessageText = string.Format(ggMessageBox.MessageText, dgv.SelectedRows.Count.ToString()); string[] argsArray = new string[100]; argsArray[0] = dgv.SelectedRows.Count.ToString(); ggMessageBox.MessageText = string.Format(ggMessageBox.MessageText, argsArray); //if (DialogResult.OK == MessageBox.Show("WARNING!!! You are about to permanently delete " + dgv.SelectedRows.Count.ToString() + " records from the central database!\n\nAre you sure you want to do this?", "Record delete confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)) if (DialogResult.OK == ggMessageBox.ShowDialog()) { foreach (DataGridViewRow dgvr in dgv.SelectedRows) { dgv.Rows.Remove(dgvr); } } e.Handled = true; } } // Restore cursor to default cursor... Cursor.Current = origCursor; return keyProcessed; }
public bool ProcessDGVReadOnlyShortcutKeys(DataGridView dgv, KeyEventArgs e, string cno, LookupTables lookupTables) { bool keyProcessed = false; // Change cursor to the wait cursor... Cursor origCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; if (e.KeyCode == Keys.C && e.Control) { string copyString = ""; // First we need to get the min/max rows and columns for the selected cells... int minCol = dgv.Columns.Count; int maxCol = -1; int minRow = dgv.Rows.Count; int maxRow = -1; // Processing keystroke... keyProcessed = true; foreach (DataGridViewCell dgvc in dgv.SelectedCells) { if (dgvc.ColumnIndex < minCol) minCol = dgvc.ColumnIndex; if (dgvc.ColumnIndex > maxCol) maxCol = dgvc.ColumnIndex; if (dgvc.RowIndex < minRow) minRow = dgvc.RowIndex; if (dgvc.RowIndex > maxRow) maxRow = dgvc.RowIndex; } // First, gather the column headers (but only if the entire row was selected)... if (dgv.SelectedRows.Count != 0) { for (int i = minCol; i <= maxCol; i++) { copyString += dgv.Columns[i].HeaderText + '\t'; //copyString += dgv.Columns[i].Name + '\t'; } // Strip the last tab and insert a newline... copyString = copyString.TrimEnd('\t'); copyString += "\r\n"; } // Now build the string to pass to the clipboard... for (int i = minRow; i <= maxRow; i++) { for (int j = minCol; j <= maxCol; j++) { switch (dgv[j, i].FormattedValueType.Name) { case "Boolean": copyString += dgv[j, i].Value.ToString() + '\t'; break; default: if (dgv[j, i].FormattedValue == null || dgv[j, i].FormattedValue.ToString().ToLower() == "[null]") { copyString += "" + '\t'; } else { copyString += dgv[j, i].FormattedValue.ToString() + '\t'; } break; } } copyString = copyString.TrimEnd('\t'); copyString += "\r\n"; } copyString = copyString.TrimEnd('\n'); copyString = copyString.TrimEnd('\r'); // Pass the new string to the clipboard... Clipboard.SetDataObject(copyString, false, 1, 1000); } if (e.KeyCode == Keys.E && e.Control) { // Processing keystroke... keyProcessed = true; string currentCellValue = dgv.CurrentCell.Value.ToString(); RichTextEditor rte = new RichTextEditor(currentCellValue, true); rte.ShowDialog(); } // Restore cursor to default cursor... Cursor.Current = origCursor; return keyProcessed; }