How do I get DataGridView comboboxes to display their drop down list in one click?

前端 未结 4 1325
故里飘歌
故里飘歌 2021-01-06 02:42

After I set \"EditOnEnter\" to be true, the DataGridViewComboBoxCell still takes two clicks to open if I don\'t click on the down arrow part of the combo box.

4条回答
  •  情话喂你
    2021-01-06 03:31

    I used this solution as it avoids sending keystrokes:

    Override the OnCellClick method (if you're subclassing) or subscribe to the CellClick event (if you're altering the DGV from another object rather than as a subclass).

    protected override void OnCellClick(DataGridViewCellEventArgs e)
    {
        // Normally the user would need to click a combo box cell once to 
        // activate it and then again to drop the list down--this is annoying for 
        // our purposes so let the user activate the drop-down with a single click.
        if (e.ColumnIndex == this.Columns["YourDropDownColumnName"].Index
            && e.RowIndex >= 0
            && e.RowIndex <= this.Rows.Count)
        {
            this.CurrentCell = this[e.ColumnIndex, e.RowIndex];
            this.BeginEdit(false);
            ComboBox comboBox = this.EditingControl as ComboBox;
            if (comboBox != null)
            {
                comboBox.DroppedDown = true;
            }
        }
    
        base.OnCellContentClick(e);
    }
    

提交回复
热议问题