Direct access to DataGridView combobox in one click?

后端 未结 2 613
执念已碎
执念已碎 2020-12-10 01:08

I\'m getting annoyed with clicking once to select a row in the datagridview, and then clicking again to click on a control in that row (in this case a combobox).

Is

相关标签:
2条回答
  • 2020-12-10 01:44

    Change the EditMode property of your DataGridView control to "EditOnEnter". This will affect all columns though.

    0 讨论(0)
  • 2020-12-10 02:03

    If you want to selectively apply the one-click editing to certain columns, you can switch the current cell during the MouseDown event to eliminate the click to edit:

    // Subscribe to DataGridView.MouseDown when convenient
    this.dataGridView.MouseDown += this.HandleDataGridViewMouseDown;
    
    private void HandleDataGridViewMouseDown(object sender, MouseEventArgs e)
    {
        // See where the click is occurring
        DataGridView.HitTestInfo info = this.dataGridView.HitTest(e.X, e.Y);
    
        if (info.Type == DataGridViewHitTestType.Cell)
        {
            switch (info.ColumnIndex)
            {
                // Add and remove case statements as necessary depending on
                // which columns have ComboBoxes in them.
    
                case 1: // Column index 1
                case 2: // Column index 2
                    this.dataGridView.CurrentCell =
                        this.dataGridView.Rows[info.RowIndex].Cells[info.ColumnIndex];
                    break;
                default:
                    break;
            }
        }
    }
    

    Of course, if your columns and their indexes are dynamic, you would need to modify this a bit.

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