I've got a form with a DataGridView. In the Datagridview I have some DataGridViewComboBoxColumn and some DataGridViewTextBoxColumn. The problem is that I want to use Enter instead of Tab to switch from one cell to another, even when I'm in editmode in the cell.
screenshot of the datagridview to customize
screenshot of the combobox to customize
I succeded in implementing the solution provided in this answer https://stackoverflow.com/a/9917202/249120 for the Textbox Columns, but I can't implement it for a Combobox Column. How to do it?
Important:for the Textbox Columns the defaultCellStyle must have the property "wrap" to True. It worked (when you press enter it's like you are pressing a tab) so I decided to test it also with a "CustomComboBoxColumn" and I tried to create a code very similar to the one for the CustomTextBoxColumn , but it doesn't work :
#region CustomComboBoxColumn
public class CustomComboBoxColumn : DataGridViewColumn
{
public CustomComboBoxColumn() : base(new CustomComboBoxCell()) { }
public override DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomComboBoxCell)))
{
throw new InvalidCastException("Must be a CustomComboBoxCell");
}
base.CellTemplate = value;
}
}
}
public class CustomComboBoxCell : DataGridViewComboBoxCell
{
public override Type EditType
{
get { return typeof(CustomComboBoxEditingControl); }
}
}
public class CustomComboBoxEditingControl : DataGridViewComboBoxEditingControl
{
protected override void WndProc(ref Message m)
{
//we need to handle the keydown event
if (m.Msg == Native.WM_KEYDOWN)
{
if ((ModifierKeys & Keys.Shift) == 0)//make sure that user isn't entering new line in case of warping is set to true
{
Keys key = (Keys)m.WParam;
if (key == Keys.Enter)
{
if (this.EditingControlDataGridView != null)
{
if (this.EditingControlDataGridView.IsHandleCreated)
{
//sent message to parent dvg
Native.PostMessage(this.EditingControlDataGridView.Handle, (uint)m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
m.Result = IntPtr.Zero;
}
return;
}
}
}
}
base.WndProc(ref m);
}
}
#endregion
The final result of this is a ComboBoxColumn without the properties DataSource, DisplayMember, Items, ValueMember, but when pressing the Enter goes to the next cell. What else to do?