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
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.
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;
}
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.
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.
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 -
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
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;
}
}