DatagridViewComboBox: How to indicate dropdown?

dranko

Member
Joined
Apr 16, 2006
Messages
16
Programming Experience
Beginner
Regular ComboBox control has a property DroppedDown (Gets or sets a value indicating whether the combo box is displaying its drop-down portion).

DatagridViewComboBox sims to doesn't have that property.
How to indicate whether the DatagridViewComboBox is displaying its drop-down portion?
 
You need to be careful with your terminology. There is no such type as DataGridViewComboBox. There is a DatagridViewComboBoxColumn, which is a DataGridViewColumn that contains DataGridViewComboBoxCells, which are DataGridViewCells that contain DataGridViewComboBoxEditingControls. The DataGridViewComboBoxEditingControl class inherits ComboBox and implements IDataGridViewEditingControl. It is the actual ComboBox that gets displayed so you need to get its DroppedDown property. The column and the cells only control how the ComboBox will look when it's displayed. What you need to do is get a reference to the DataGridViewComboBoxEditingControl currently displayed and then get its DropedDown property:
VB.NET:
'If the current editing control is a DataGridViewComboBoxEditingControl this will return a reference to it.
'If there is no current editing control or it is a different type then this will return Nothing.
Dim editingControl As ComboBox = TryCast(Me.DataGridView1.EditingControl, ComboBox)

If editingControl Is Nothing Then
    Debug.WriteLine("There is no combo box currently in use.")
ElseIf editingControl.DroppedDown Then
    Debug.WriteLine("The combo box list is dropped down.")
Else
    Debug.WriteLine("The combo box list is not dropped down.")
End If
 
jmcilhinney said:
You need to be careful with your terminology. There is no such type as DataGridViewComboBox. There is a DatagridViewComboBoxColumn, which is a DataGridViewColumn that contains DataGridViewComboBoxCells, which are DataGridViewCells that contain DataGridViewComboBoxEditingControls. The DataGridViewComboBoxEditingControl class inherits ComboBox and implements IDataGridViewEditingControl. It is the actual ComboBox that gets displayed so you need to get its DroppedDown property. The column and the cells only control how the ComboBox will look when it's displayed. What you need to do is get a reference to the DataGridViewComboBoxEditingControl currently displayed and then get its DropedDown property:
VB.NET:
'If the current editing control is a DataGridViewComboBoxEditingControl this will return a reference to it.
'If there is no current editing control or it is a different type then this will return Nothing.
Dim editingControl As ComboBox = TryCast(Me.DataGridView1.EditingControl, ComboBox)

If editingControl Is Nothing Then
    Debug.WriteLine("There is no combo box currently in use.")
ElseIf editingControl.DroppedDown Then
    Debug.WriteLine("The combo box list is dropped down.")
Else
    Debug.WriteLine("The combo box list is not dropped down.")
End If
Thank you for detailed answer, it's much clearer for me now.
I want to do some action when combo box list is droppped down. Combobox has event DropDown (Occurs when the drop-down portion of a ComboBox is shown).
I nedd to handle that event, but I don't how to do it with DataGridViewComboBoxEditingControl (to add an EventHandler).
This is maybe trivial but I am beginner, so please give me some code example.
Thanks
 
I've been doing some research into types of columns in the DataGridView lately myself because I'm creating some custom columns, e.g. NumericUpDown, DateTimePicker, MaskedTextBox, etc. and it's really quite involved. When the DataGridView is just sitting around doing nothing there are actually no ComboBox objects at all. What you see in the grid cells are Bitmaps containing the view of a ComboBox. A ComboBox is only created when you click in the cell and start editing. If you tehn click in another cell it actually uses the same ComboBox object to avoid unnecessary object destruction and recreation. If you leave the column and then click back in the same cell, it is actually a completely new ComboBox object that you see. The upshot of all this is that you cannot simply attach an event handler to a specific ComboBox object and expect it to be executed. I'm by no means an expert in this area as yet but I think what you would have to do is write your DropDown event handler but not attach it to any control. You'd then handle the EditingControlShowing event of the DataGridView and then attach your method to the control that is showing.
VB.NET:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)

    If editingComboBox IsNot Nothing Then
        AddHandler editingComboBox.DropDown, AddressOf EditingComboBox_DropDown
    End If
End Sub

Private Sub EditingComboBox_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Debug.WriteLine("A ComboBox in the DataGridView just dropped down.")
End Sub
 
That works.
Sorry to bother you again, I have an idea, something like multicolumncombobox in DataGridView, but I don't know how to realize it.
I have DataGridView bound to datatable and some columns. Two columns are DataGridViewComboBoxColumn type and have the same ValueMember (ProductID) and different DisplayMember (for example ProductID and ProductName).
What I want is when first combo is droppeddown that second combo is droppeddown at the same time and when I move through first combo list that selection moves through second list to, and showing corresponding ProductID and ProductName. Then, when I choose ProductID from first combo to close both combos.
I hope you understand what I mean, because English is not my native language.
 
If your two columns are synchronised then why have them in separate columns at all? Even if they are in separate columns in the database doesn't mean that they have to be in the grid. You could combine the two columns using your SQL query, add an additional calculated column to your data table or add an unbvound column to your grid. There are multiple options that would be preferable to what you describe.
 
Back
Top