30

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 there a way configure this thing so that all this can be done in one mouse click instead of two?

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90
  • You may want to check [this solution.](http://stackoverflow.com/questions/34543940/datagridviewcomboboxcolumn-doesnt-open-the-dropdown-on-first-click/39757746#39757746) – TaW Sep 28 '16 at 21:18

2 Answers2

55

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

Stuart Helwig
  • 9,318
  • 8
  • 51
  • 67
  • Works just as I hoped. Thanks Stuart! – Isaac Bolinger Aug 10 '10 at 00:16
  • An even better solution is posted on Microsoft's Forums. It places the cursor right in the middle of the text just like I wanted: http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/fe5d5cfb-63b6-4a69-a01c-b7bbd18ae84a – HK1 Oct 19 '12 at 15:56
  • I ended up selecting all the text on click, and subclassing the combobox to have autocomplete. It was a lot of work though – Isaac Bolinger Dec 03 '14 at 15:52
3

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.

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86