Category / Section
How to have two or more columns with column headers in a DropDown cell in WinForms GridGroupingControl?
2 mins read
Drop down cell
To have two or more columns with column headers in a dropdown cell, the GridListControl cell type can be used. ShowColumnHeader property will be used for displaying the column headers along with the following code.
C#
//To add columns
GridTableCellStyleInfo styleInfo = this.gridGroupingControl1.TableDescriptor.Columns["CategoryName"].Appearance.AnyRecordFieldCell;
styleInfo.DataSource = GetTable();
styleInfo.ValueMember = "ID";
styleInfo.DisplayMember = "Value";
styleInfo.CellType = "GridListControl";
GridDropDownGridListControlCellRenderer gcr1 = (GridDropDownGridListControlCellRenderer)gridGroupingControl1.TableControl.CellRenderers["GridListControl"];
gcr1.ListControlPart.ShowColumnHeader = true;
styleInfo.DropDownStyle = GridDropDownStyle.AutoComplete;
VB
'To add columns
Dim styleInfo As GridTableCellStyleInfo = Me.gridGroupingControl1.TableDescriptor.Columns("CategoryName").Appearance.AnyRecordFieldCell
styleInfo.DataSource = GetTable()
styleInfo.ValueMember = "ID"
styleInfo.DisplayMember = "Value"
styleInfo.CellType = "GridListControl"
Dim gcr1 As GridDropDownGridListControlCellRenderer = CType(gridGroupingControl1.TableControl.CellRenderers("GridListControl"), GridDropDownGridListControlCellRenderer)
gcr1.ListControlPart.ShowColumnHeader = True
styleInfo.DropDownStyle = GridDropDownStyle.AutoComplete
TableControlCurrentCellShowingDropDown event triggers when the dropdown list opened.
C#
this.gridGroupingControl1.TableControlCurrentCellShowingDropDown += new GridTableControlCurrentCellShowingDropDownEventHandler(gridGroupingControl1_TableControlCurrentCellShowingDropDown);
void gridGroupingControl1_TableControlCurrentCellShowingDropDown(object sender, GridTableControlCurrentCellShowingDropDownEventArgs e)
{
GridDropDownGridListControlCellRenderer cr = this.gridGroupingControl1.TableControl.CurrentCell.Renderer as GridDropDownGridListControlCellRenderer;
if (cr != null)
((GridDropDownGridListControlPart)cr.ListControlPart).DropDownRows = 5;
}
VB
Private Me.gridGroupingControl1.TableControlCurrentCellShowingDropDown += New GridTableControlCurrentCellShowingDropDownEventHandler(AddressOf gridGroupingControl1_TableControlCurrentCellShowingDropDown)
Private Sub gridGroupingControl1_TableControlCurrentCellShowingDropDown(ByVal sender As Object, ByVal e As GridTableControlCurrentCellShowingDropDownEventArgs)
Dim cr As GridDropDownGridListControlCellRenderer = TryCast(Me.gridGroupingControl1.TableControl.CurrentCell.Renderer, GridDropDownGridListControlCellRenderer)
If cr IsNot Nothing Then
CType(cr.ListControlPart, GridDropDownGridListControlPart).DropDownRows = 5
End If
End Sub
Screenshot
Samples:
C#: DropDownCell
VB: DropDownCell