Articles in this section
Category / Section

How to determine the Drop-Down items in a Grid Combo Box cell in WinForms GridControl?

2 mins read

Drop-down items in grid

You can set the DropDownRows property to control this. But it is buried a little deep, and generally requires an event handler to set it.

As, a single combo box cell control is shared among all combo box cells normally. And each cell can potentially have a different list and may require different drop-down rows.

Solution

To handle this, use the CurrentCellShowingDropdown event, and set the property there depending upon the exact row and column. Following are some code examples.

C#

//set the celltype into combobox.
this.gridControl1[6, 4].CellType = "ComboBox";
this.gridControl1[6, 4].CellValue = "None";
this.gridControl1[6, 4].ShowButtons = GridShowButtons.ShowCurrentCell;
this.gridControl1[6, 4].CellAppearance = GridCellAppearance.Raised;
this.gridControl1[6, 4].ChoiceList = items;
//Trigger the event
this.gridControl1.CurrentCellShowingDropDown += gridControl1_CurrentCellShowingDropDown;
void gridControl1_CurrentCellShowingDropDown(object sender, Syncfusion.Windows.Forms.Grid.GridCurrentCellShowingDropDownEventArgs e)
{
    GridControlBase cb = sender as GridControlBase;
    if (cb != null)
    {
        GridCurrentCell cc = cb.CurrentCell;
        GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
        //check the GridComboBoxCellRenderer is not null
        if (cr != null)
        {
            if (cc.RowIndex == 6)
                //Show the dropdown as 4 when the row index as 6
                ((GridComboBoxListBoxPart)cr.ListBoxPart).DropDownRows = 4;
            else if (cc.RowIndex == 2)
                //Show the dropdown as 2 when the row index as 2
                ((GridComboBoxListBoxPart)cr.ListBoxPart).DropDownRows = 2;
            //Show the dropdown as 6 when the row index as 4
            else ((GridComboBoxListBoxPart)cr.ListBoxPart).DropDownRows = 6;
        }
    }
}

VB

'set the celltype into combobox.
Me.gridControl1(6, 4).CellType = "ComboBox"
Me.gridControl1(6, 4).CellValue = "None"
Me.gridControl1(6, 4).ShowButtons = GridShowButtons.ShowCurrentCell
Me.gridControl1(6, 4).CellAppearance = GridCellAppearance.Raised
Me.gridControl1(6, 4).ChoiceList = items
'Trigger the event
Me.gridControl1.CurrentCellShowingDropDown += gridControl1_CurrentCellShowingDropDown
Private Sub gridControl1_CurrentCellShowingDropDown(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCurrentCellShowingDropDownEventArgs)
Dim cb As GridControlBase = TryCast(sender, GridControlBase)
If cb IsNot Nothing Then
    Dim cc As GridCurrentCell = cb.CurrentCell
    Dim cr As GridComboBoxCellRenderer = TryCast(cc.Renderer, GridComboBoxCellRenderer)
    'check the GridComboBoxCellRenderer is not null
    If cr IsNot Nothing Then
        If cc.RowIndex = 6 Then
            'Show the dropdown as 4 when the row index as 6
            CType(cr.ListBoxPart, GridComboBoxListBoxPart).DropDownRows = 4
        ElseIf cc.RowIndex = 2 Then
        'Show the dropdown as 2 when the row index as 2
        CType(cr.ListBoxPart, GridComboBoxListBoxPart).DropDownRows = 2
        'Show the dropdown as 6 when the row index as 4
        Else
            CType(cr.ListBoxPart, GridComboBoxListBoxPart).DropDownRows = 6
        End If
    End If
End If
End Sub

Samples:

C#: Dropdown_Control

VB: Dropdown_Control

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied