Category / Section
How to change the dropdown height in WinForms GridGroupingControl?
3 mins read
Customize the dropdown height
You can
customize the dropdown height of the ComboBox cell using the TableControlCurrentCellShowingDropDown event.
The size can be set for the dropdown based on the items count and its default
height.
C#
this.gridGroupingControl1.QueryCellStyleInfo += new GridTableCellStyleInfoEventHandler(gridGroupingControl1_QueryCellStyleInfo);
this.gridGroupingControl1.TableControlCurrentCellShowingDropDown += new GridTableControlCurrentCellShowingDropDownEventHandler(gridGroupingControl1_TableControlCurrentCellShowingDropDown);
void gridGroupingControl1_TableControlCurrentCellShowingDropDown(object sender, GridTableControlCurrentCellShowingDropDownEventArgs e)
{
if(this.gridGroupingControl1.TableModel.CurrentCellRenderer != null && this.gridGroupingControl1.TableModel.CurrentCellRenderer is GridDropDownGridListControlCellRenderer)
{
int defaultHeight=18;
GridDropDownGridListControlCellRenderer listRenderer = (GridDropDownGridListControlCellRenderer)this.gridGroupingControl1.TableModel.CurrentCellRenderer;
if(isitem1)
{
e.Inner.Size = new Size(100, (items1.Count * defaultHeight));//Set the height based on the items.
}
else if(isitem2)
{
e.Inner.Size = new Size(100, (items2.Count * defaultHeight));//Set the height based on the items.
}
listRenderer.ListControlPart.ShowColumnHeader = false;
}
}
void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
double result = 0;
if(e.TableCellIdentity.Column != null)
{
if(e.TableCellIdentity.Column.GetName().Equals("PaymentType") && e.TableCellIdentity.DisplayElement.GetRecord() != null)
{
If (double.TryParse(e.TableCellIdentity.DisplayElement.GetRecord().GetValue("Amount").ToString(), out result))
{
//Bind the DataSource for drop down combo box cell based on the ‘Amount’ column.
if(result > 0)
{
e.Style.DataSource = items1;
isitem2 = false;
isitem1 = true;
}
else
{
e.Style.DataSource = items2;
isitem2 = true;
isitem1 = false;
}
}
e.Style.CellType = GridCellTypeName.GridListControl;//To set Combobox Celltype.
e.Style.ShowButtons = GridShowButtons.Show;
e.Style.DropDownStyle = GridDropDownStyle.Editable;//This allows you to edit the combo box contents.
e.Style.AutoCompleteInEditMode = GridComboSelectionOptions.AutoSuggest;//Auto complete support for combobox in edit mode.
}
}
}
VB
AddHandler gridGroupingControl1.QueryCellStyleInfo, AddressOf gridGroupingControl1_QueryCellStyleInfo
AddHandler gridGroupingControl1.TableControlCurrentCellShowingDropDown, AddressOf gridGroupingControl1_TableControlCurrentCellShowingDropDown
Private Sub gridGroupingControl1_TableControlCurrentCellShowingDropDown(ByVal sender As Object, ByVal e As GridTableControlCurrentCellShowingDropDownEventArgs)
If Me.gridGroupingControl1.TableModel.CurrentCellRenderer IsNot Nothing AndAlso TypeOf Me.gridGroupingControl1.TableModel.CurrentCellRenderer Is GridDropDownGridListControlCellRenderer Then
Dim defaultHeight As Integer=18
Dim listRenderer As GridDropDownGridListControlCellRenderer = CType(Me.gridGroupingControl1.TableModel.CurrentCellRenderer, GridDropDownGridListControlCellRenderer)
If isitem1 Then
e.Inner.Size = New Size(100, (items1.Count * defaultHeight)) ‘Set the height based on the items.
ElseIf isitem2 Then
e.Inner.Size = New Size(100, (items2.Count * defaultHeight)) ‘Set the height based on the items.
End If
listRenderer.ListControlPart.ShowColumnHeader = False
End If
End Sub
Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As GridTableCellStyleInfoEventArgs)
Dim result As Double = 0
If e.TableCellIdentity.Column IsNot Nothing Then
If e.TableCellIdentity.Column.GetName().Equals("PaymentType") AndAlso e.TableCellIdentity.DisplayElement.GetRecord() IsNot Nothing Then
If Double.TryParse(e.TableCellIdentity.DisplayElement.GetRecord().GetValue("Amount").ToString(), result) Then
‘Bind the DataSource for drop down combo box cell based on the ‘Amount’ column.
If result > 0 Then
e.Style.DataSource = items1
isitem2 = False
isitem1 = True
Else
e.Style.DataSource = items2
isitem2 = True
isitem1 = False
End If
End If
e.Style.CellType = GridCellTypeName.GridListControl 'To set Combobox Celltype.
e.Style.ShowButtons = GridShowButtons.Show
e.Style.DropDownStyle = GridDropDownStyle.Editable 'This allows the user to edit the combo box contents.
e.Style.AutoCompleteInEditMode = GridComboSelectionOptions.AutoSuggest 'Auto complete support for combobox in edit mode.
End If
End If
End Sub
The following
screenshot displays a ComboBox cell with customized dropdown height.
Figure 1: Output
Samples:
C#: DynamicComboBox
VB: DynamicComboBox