-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?

4

2 に答える 2

1

ブルーノ・パシフィチの答えへの答えとコメント:

または、同じことを行う 1 つのメソッド「ProcessCmdKey」のみをオーバーライドできます。

    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);
    }

PS回答が大きすぎない場合、回答としてリンクのみを投稿するのはなぜですか? そのような「役立つ」リンクが機能しなくなったというケースを、私は非常に多く経験してきました。したがって、元のソースへのリンクを含むコードをコピーすると、より「404」セーフな回答になります。

于 2015-11-12T21:05:07.907 に答える
0

私の質問に対する解決策は信じられないほど単純です。たった 2 つのメソッドをオーバーライドするカスタム dataGridView を作成する必要があります。ここを参照してください: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq9 .

于 2012-12-30T10:49:08.773 に答える