-1

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?

Community
  • 1
  • 1
  • Can you add some information about what you have tried? – jeremy Dec 27 '12 at 21:54
  • i'm not sure what the problem is?? i think when you select an item of combobox it move to next cell, if so? does this happen when using mouse also or when pressing enter key only?? i advise you to check the selection change event. – ahmedsafan86 Dec 28 '12 at 21:54
  • the comboboxcolumn has displaystyle: nothing and flatstyle: flat .if I select an item and I press enter it works, if I select an item using mouse it goes to the next row, if I write the value manually (It's what I want to do really) and I press ENTER it goes to the next row. – Bruno Pacifici Dec 29 '12 at 18:04

2 Answers2

1

an answer and a comment to Bruno Pacifici answer:

OR you can override only 1 method "ProcessCmdKey" which will do the same:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        //Override behavior on Enter press
        if (keyData == Keys.Enter)
        {
            if (CurrentCell != null)
            {
                if (CurrentCell.IsInEditMode)
                {
                    //Do Stuff if cell is currently being edited
                    return ProcessTabKey(keyData);
                }
                else
                {
                    //Do Stuff if cell is NOT yet currently edited
                    BeginEdit(true);
                }
            }
        }
        //Process all other keys as expected
        return base.ProcessCmdKey(ref msg, keyData);
    }

P.S. Why posting only link as an answer, if the answer is not too large? I have experienced so many cases, when such "helpful" link did not work anymore. So, copying some code with link to original source would be more "404"-safe answer.

Vadim K.
  • 331
  • 3
  • 9
0

The solution to my question is incredibly simple.You have to create a custom dataGridView overriding just 2 methods. See here : http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq9 .