20

I have a DataGridView with one DataGridViewComboBoxColumn in my WinForms application. I need to drop down (open) this DataGridViewComboBoxColumn manually, let's say after a button is clicked.

The reason I need this is I have set SelectionMode to FullRowSelect and I need to click 2-3 times to open the combo box. I want to click on the combobox cell and it should drop down immediately. I want to do this with CellClick event, or is there any other way?

I am searching in Google and VS help, but I haven't found any information yet.

Can anybody help please?

7 Answers 7

24

I know this can't be the ideal solution but it does create a single click combo box that works within the cell.

   Private Sub cell_Click(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DataGridView1.BeginEdit(True)
        If DataGridView1.Rows(e.RowIndex).Cells(ddl.Name).Selected = True Then
            DirectCast(DataGridView1.EditingControl, DataGridViewComboBoxEditingControl).DroppedDown = True
        End If
    End Sub

where "ddl" is the combobox cell I added in the gridview.

18

Thanks ThisMat, your solution works perfectly.

My code in C#:

private void dataGridViewWeighings_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (e.RowIndex < 0) {
        return;     // Header
    }
    if (e.ColumnIndex != 5) {
        return;     // Filter out other columns
    }

    dataGridViewWeighings.BeginEdit(true);
    ComboBox comboBox = (ComboBox)dataGridViewWeighings.EditingControl;
    comboBox.DroppedDown = true;
}
0
11

I have been able to get close to what you're looking for by setting

DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter

As long as no other cell's dropdown is shown it should display the selected cell's dropdown immediately.

I'll keep thinking and update if anything comes up.

3

Thanks for the C# version. Here's my contribution to search by combo column names:

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    string Weekdays = @"MondayTuesdayWednesdayThursdayFridaySaturdaySunday";
    if (Weekdays.IndexOf(dgv.Columns[e.ColumnIndex].Name) != -1)
    {
        dgv.BeginEdit(true);
        ComboBox comboBox = (ComboBox)dgv.EditingControl;
        comboBox.DroppedDown = true;
    }
}
2

I was looking for an answer to this as well. I ended up writing a generic sub that could be called from any DataGridView since I had plenty in my apps and I wanted them all to behave the same way. This worked well for me so I wanted to share it with anyone else who stumbled across this post.

In the MouseClick event for the DGV I add the code

Private Sub SomeGrid_MouseClick(sender As Object, e As MouseEventArgs) Handles SomeGrid.MouseClick
    DGV_MouseClick(sender, e)
End Sub

Which calls the following sub that I store in a shared module

Public Sub DGV_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Try
        Dim dgv As DataGridView = sender
        Dim h As DataGridView.HitTestInfo = dgv.HitTest(e.X, e.Y)
        If h.RowIndex > -1 AndAlso h.ColumnIndex > -1 AndAlso dgv.Columns(h.ColumnIndex).CellType Is GetType(DataGridViewComboBoxCell) Then
            Dim cell As DataGridViewComboBoxCell = dgv.Rows(h.RowIndex).Cells(h.ColumnIndex)
            If Not dgv.CurrentCell Is cell Then dgv.CurrentCell = cell
            If Not dgv.IsCurrentCellInEditMode Then
                dgv.BeginEdit(True)
                CType(dgv.EditingControl, ComboBox).DroppedDown = True
            End If
        End If
    Catch ex As Exception
    End Try
End Sub

I never caught any errors, I only include the Try..Catch code for some rare instance I couldn't think of that might throw an exception. I didn't want the user bothered by error messages for this scenario. If the sub fails, then most likely the DGV will just behave like it normally does anyways.

2

I was able to activate combo box and drop down it using a single mouse click by setting EditMode property of DataGridView to EditOnEnter and creating EditingControlShowing event and added code to drop down the combo box in this event. Here is the sample code -

//to get the correct cell get value of row and column indexs of the cell
 ColIndex = 1;
 RowIndex = 1;

 DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
 ComboBoxCell.Items.AddRange("XYZ", "ABC", "PQR");
 ComboBoxCell.Value = "XYZ";
 datagridview1[ColIndex, RowIndex] = ComboBoxCell;

From the above code DataGirdCell at the location (1,1) will be converted to a "DataGridViewComboBoxCell" and combo box will be shown in the cell.

It might be possible that to dropdown the combo box multiple mouse clicks are required. To activate combo box on single click following steps are required -

  1. Set ReadOnly property of the combobox cell to false
  2. Set EditMode property of DataGridView to EditOnEnter
  3. Create EditingControlShowing event and add code to drop down the combo box

Here is the sample code to drop down the combo box and activate it on single click -

private void datagridview1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
ComboBox ctrl = e.Control as ComboBox;
ctrl.Enter -= new EventHandler(ctrl_Enter);
ctrl.Enter += new EventHandler(ctrl_Enter);        
}
void ctrl_Enter(object sender, EventArgs e)
{
(sender as ComboBox).DroppedDown = true;
}

For more detail please check - http://newapputil.blogspot.in/2015/08/add-combo-box-in-cell-of-datagridview.html

1
  • Please provide the code here to preserve its life cycle in context with your comment. Anyway, I looked at your code and think it is interesting and might work as long as the DGV has Only ComboBox cells.
    – gridtrak
    May 10, 2018 at 15:58
0

FYI: Here is nvivekgoyal's code from the reference in his answer:

private void datagridview1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox ctrl = e.Control as ComboBox;
    ctrl.Enter -= new EventHandler(ctrl_Enter);
    ctrl.Enter += new EventHandler(ctrl_Enter);
}

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.