Open dropdown(in a datagrid view) items on a single click

前端 未结 3 1430
孤街浪徒
孤街浪徒 2020-12-01 20:39

How can i avoid the double click on a DropDownButton used within a DataGridView? Right now I am able to view the drop down items within the D

相关标签:
3条回答
  • 2020-12-01 21:39

    You can achieve this by subscribing for the EditingControlShowing event of the grid and there for control of type ComboBox

    ComboBox ctl = e.Control as ComboBox;
    ctl.Enter -= new EventHandler(ctl_Enter);
    ctl.Enter += new EventHandler(ctl_Enter);
    

    And in the Enter event, use the property

    void ctl_Enter(object sender, EventArgs e)
    {
        (sender as ComboBox).DroppedDown = true;
    }
    

    DroppedDown indicates as the name suggests whether the dropdown area is shown or not, so whenever the control is entered this will set it to true and display the items without the need of further clicks.

    0 讨论(0)
  • 2020-12-01 21:39

    The "set EditMode property of the DataGridView to EditOnEnter" worked for me, but I found another problem: user can't delete a row by just selecting and pressing DEL key. So, a google search gave me another way to do it. Just catch the event CellEnter and check if the cell is the appropriated type to perform appropriated action like this sample code:

    private void Form_OnLoad(object sender, EventArgs e){
        dgvArmazem.CellEnter += new DataGridViewCellEventHandler(dgvArmazem_CellEnter);
    }
    
    void dgvArmazem_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                DataGridView dg = (DataGridView)sender;
    
                if (dg.CurrentCell.EditType == typeof(DataGridViewComboBoxEditingControl))
                {
                    SendKeys.Send("{F4}");
                }
            }
    

    Now the ComboBox drops down faster and the user still delete a row by selecting a row and pressing DEL key.

    That's it.

    0 讨论(0)
  • 2020-12-01 21:40

    Set EditMode property of the DataGridView to EditOnEnter: link

    DataGridView.EditMode - Gets or sets a value indicating how to begin editing a cell.

    EditOnEnter - Editing begins when the cell receives focus.

    0 讨论(0)
提交回复
热议问题