public sealed class CustomDataGridView : DataGridView { public CustomDataGridView() { AllowDrop = false; AllowUserToResizeColumns = false; AllowUserToResizeRows = false; RightToLeft = RightToLeft.No; EnableHeadersVisualStyles = false; ScrollBars = ScrollBars.Vertical; CurrentCellDirtyStateChanged += OnCurrentCellDirtyStateChanged; } private void OnCurrentCellDirtyStateChanged(object sender, EventArgs e) { var dataGridView = sender as DataGridView; if (dataGridView != null) { if (dataGridView.CurrentRow != null && !dataGridView.CurrentRow.IsNewRow) { if (dataGridView.CurrentCell != null) { if (dataGridView.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell)) { if (dataGridView.IsCurrentCellDirty) { var inputValue = ((string)dataGridView.CurrentCell.EditedFormattedValue); MessageBox.Show("Selected value is: " + inputValue); } } } } } } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Enter) { if (CurrentCell != null) { if (CurrentCell.GetType() == typeof(DataGridViewComboBoxCell)) { if (CurrentCell.IsInEditMode) { var comboBox = EditingControl as DataGridViewComboBoxEditingControl; if (comboBox != null) { //If cell is edited and combobox is shown, then on Enter press - select current item if (comboBox.DroppedDown) { var inputValue = comboBox.GetItemText(comboBox.SelectedItem); comboBox.DroppedDown = false; comboBox.SelectedIndex = comboBox.Items.IndexOf(inputValue); EndEdit(); NotifyCurrentCellDirty(true); return true; } } } else { //If cell is selected, then on Enter press - open combobox drop down BeginEdit(false); var comboBox = EditingControl as DataGridViewComboBoxEditingControl; if (comboBox != null) { comboBox.SelectedIndex = 0; comboBox.DropDownStyle = ComboBoxStyle.DropDownList; comboBox.Focus(); comboBox.DroppedDown = true; } return true; } } } } //Process all other keys as expected return base.ProcessCmdKey(ref msg, keyData); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)