Articles in this section
Category / Section

How to insert a ComboBox in a header cell in WinForms GridGroupingControl?

8 mins read

Combobox in a header cell of grid

GridControl

You can place a ComboBox in a header cell of a WinForms Grid Control by setting the CellType of the header to ComboBox and assign a valid datasource (Choicelist or DataSource). Refer to the following code examples.

C#

//set the celltype into combobox.
this.gridControl1[0, 4].CellType = "ComboBox";
this.gridControl1[0, 4].CellValue = "combo";
//set the GridShowButtons as Show option for showing the combobox in the normal view. The combobox is modified like currentcellediting by assigning the GridShowButtons as CurrentCellEditing
this.gridControl1[0, 4].ShowButtons = GridShowButtons.Show;
this.gridControl1[0, 4].CellAppearance = GridCellAppearance.Raised;
this.gridControl1[0, 4].ChoiceList = items;

VB

'set the celltype into combobox.
Me.gridControl1(0, 4).CellType = "ComboBox"
Me.gridControl1(0, 4).CellValue = "combo"
Me.gridControl1(0, 4).ShowButtons = GridShowButtons.ShowCurrentCellEditing
Me.gridControl1(0, 4).CellAppearance = GridCellAppearance.Raised
Me.gridControl1(0, 4).ChoiceList = items

GridDataBoundGrid

You can place a ComboBox in the header cell of a GridDataBoundGrid by handling QueryCellInfo and SaveCellInfo events. Unlike GridControl, the GridDataBoundGrid does not maintain any internal datastore. It reflects the data from the underlying DataTable. So set the Choicelist or DataSource datasource for the header cell in the QueryCellInfo event handler and save the edited value back to datasource in the SaveCellInfo event handler. Refer to the following code examples.

C#

this.gridDataBoundGrid1.Model.QueryCellInfo += Model_QueryCellInfo;
this.gridDataBoundGrid1.Model.SaveCellInfo += Model_SaveCellInfo;
this.gridDataBoundGrid1.CurrentCellCloseDropDown += gridDataBoundGrid1_CurrentCellCloseDropDown;
this.gridDataBoundGrid1.Leave += gridDataBoundGrid1_Leave;
//Saving styles, celltype and cell value to column header Column4
void Model_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e)
{
    if (e.ColIndex > 0 && e.RowIndex == 0)
    {
        //set the index of the column4 to the colIndex2
        int colIndex2 = this.gridDataBoundGrid1.Binder.NameToColIndex("Column4");
        if (colIndex2 == e.ColIndex)
        {
            if (e.Style.CellValue != null)
                choice_GDBG = (string)e.Style.CellValue; 
//retrived cell value is refreshed through QueryCellInfo event
        }
    }
}
//Setting styles, celltype and cell value to column header Column4
void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
    if (e.ColIndex > 0 && e.RowIndex == 0)
    {
        int colIndex2 = this.gridDataBoundGrid1.Binder.NameToColIndex("Column4");
        if (colIndex2 == e.ColIndex)
        {
            e.Style.Borders.All = border;
            e.Style.BackColor = Color.White;
            e.Style.CellType = "ComboBox";
            e.Style.ChoiceList = items;
            e.Style.CellValue = choice_GDBG;
            e.Style.CellAppearance = GridCellAppearance.Raised;
            e.Style.Enabled = true;
        }
    }
}      
//Reseting the current cell while leaving
void gridDataBoundGrid1_Leave(object sender, EventArgs e)
{
    this.gridDataBoundGrid1.CurrentCell.MoveTo(-1, -1);
}
//Indicating the close type of current cell dropdown.
void gridDataBoundGrid1_CurrentCellCloseDropDown(object sender, Syncfusion.Windows.Forms.PopupClosedEventArgs e)
{
    Console.WriteLine(e.PopupCloseType.ToString());
}

 

VB

Private Me.gridDataBoundGrid1.Model.QueryCellInfo += AddressOf Model_QueryCellInfo
Private Me.gridDataBoundGrid1.Model.SaveCellInfo += AddressOf Model_SaveCellInfo
Private Me.gridDataBoundGrid1.CurrentCellCloseDropDown += AddressOf gridDataBoundGrid1_CurrentCellCloseDropDown
Private Me.gridDataBoundGrid1.Leave += AddressOf gridDataBoundGrid1_Leave
'Saving styles, celltype and cell value to column header Column4
Private Sub Model_SaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs)
    If e.ColIndex > 0 AndAlso e.RowIndex = 0 Then
        'set the index of the column4 to the colIndex2
        Dim colIndex2 As Integer = Me.gridDataBoundGrid1.Binder.NameToColIndex("Column4")
        If colIndex2 = e.ColIndex Then
            If e.Style.CellValue IsNot Nothing Then
                choice_GDBG = CStr(e.Style.CellValue) 
                'retrived cell value is refreshed through QueryCellInfo event
           End If
        End If
    End If
End Sub
'Setting styles, celltype and cell value to column header Column4
Private Sub Model_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs)
    If e.ColIndex > 0 AndAlso e.RowIndex = 0 Then
        Dim colIndex2 As Integer = Me.gridDataBoundGrid1.Binder.NameToColIndex("Column4")
        If colIndex2 = e.ColIndex Then
            e.Style.Borders.All = border
            e.Style.BackColor = Color.White
            e.Style.CellType = "ComboBox"
            e.Style.ChoiceList = items
            e.Style.CellValue = choice_GDBG
            e.Style.CellAppearance = GridCellAppearance.Raised
           e.Style.Enabled = True
        End If
    End If
End Sub
'Reseting the current cell while leaving
Private Sub gridDataBoundGrid1_Leave(ByVal sender As Object, ByVal e As EventArgs)
    Me.gridDataBoundGrid1.CurrentCell.MoveTo(-1, -1)
End Sub
'Indicating the close type of current cell dropdown.
Private Sub gridDataBoundGrid1_CurrentCellCloseDropDown(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.PopupClosedEventArgs)
    Console.WriteLine(e.PopupCloseType.ToString())
End Sub

GridGroupingControl

You can place a ComboBox in the header cell of a GridGroupingControl by handling QueryCellIStylenfo and TableControlCellClick events. Unlike GridControl, the GridGroupingControl does not maintain any internal datastore. It reflects the data from the underlying DataTable. So set the Choicelist or DataSource datasource for the header cell in the QueryCellIStylenfo event handler and save the edited value back to datasource in the TableControlCellClick event handler. Refer to the following code examples.

C#

this.gridGroupingControl1.QueryCellStyleInfo += gridGroupingControl1_QueryCellStyleInfo;
this.gridGroupingControl1.TableControlCellClick += gridGroupingControl1_TableControlCellClick;
this.gridGroupingControl1.TableControlCurrentCellCloseDropDown += gridGroupingControl1_TableControlCurrentCellCloseDropDown;
//Assigning the selected value to cell while closing the dropdown.
void gridGroupingControl1_TableControlCurrentCellCloseDropDown(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlPopupClosedEventArgs e)
{
    GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(e.TableControl.CurrentCell.RowIndex, e.TableControl.CurrentCell.ColIndex);
    GridTableCellStyleInfoIdentity id = style.TableCellIdentity;
    GridCurrentCell cc = e.TableControl.CurrentCell;
    //Condition for checking the column header cell named 'Column4'
    if (e.Inner.PopupCloseType == Syncfusion.Windows.Forms.PopupCloseType.Done && id.Column != null && id.TableCellType == GridTableCellType.ColumnHeaderCell && id.Column.Name == "Column4")
    {
        //check whether the current cell is combo box or not
        if (cc.Renderer is GridComboBoxCellRenderer)
        {
            GridComboBoxCellRenderer rend = cc.Renderer as GridComboBoxCellRenderer;
            choice_GGC = rend.ListBoxPart.SelectedItem.ToString();
            e.TableControl.CurrentCell.Refresh();
        }                            
    }
}
//setting the styles, celltypes and cell value to the column header 4
void gridGroupingControl1_QueryCellStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs e)
{
    //Condition for checking the column header cell named 'Column4'
    if (e.TableCellIdentity.Column != null && e.TableCellIdentity.TableCellType == Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellType.ColumnHeaderCell
        && e.TableCellIdentity.Column.Name == "Column4")
    {
        e.Style.Borders.All = border;
        e.Style.BackColor = Color.White;
        e.Style.CellType = "ComboBox";
        e.Style.ChoiceList = items;
        e.Style.CellValue = choice_GGC;
        e.Style.CellAppearance = GridCellAppearance.Flat;
        e.Style.Enabled = true;
    }
}
//Forcing the showing of popup while clicking on cell
private void gridGroupingControl1_TableControlCellClick(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCellClickEventArgs e)
{
    //Moving the clicked cell to current cell
    e.TableControl.CurrentCell.MoveTo(e.Inner.RowIndex, e.Inner.ColIndex);
    GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex);
    GridTableCellStyleInfoIdentity id = style.TableCellIdentity;
    //Condition for checking the column header cell named 'Column4'
    if (id.Column != null && id.TableCellType == GridTableCellType.ColumnHeaderCell && id.Column.Name == "Column4" && !e.TableControl.CurrentCell.IsDroppedDown)
        e.TableControl.CurrentCell.ShowDropDown(); //Forcing popup
}

VB

Private Me.gridGroupingControl1.QueryCellStyleInfo += AddressOf gridGroupingControl1_QueryCellStyleInfo
Private Me.gridGroupingControl1.TableControlCellClick += AddressOf gridGroupingControl1_TableControlCellClick
Private Me.gridGroupingControl1.TableControlCurrentCellCloseDropDown += AddressOf gridGroupingControl1_TableControlCurrentCellCloseDropDown
'Assigning the selected value to cell while closing the dropdown.
Private Sub gridGroupingControl1_TableControlCurrentCellCloseDropDown(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlPopupClosedEventArgs)
    Dim style As GridTableCellStyleInfo = e.TableControl.GetTableViewStyleInfo(e.TableControl.CurrentCell.RowIndex, e.TableControl.CurrentCell.ColIndex)
    Dim id As GridTableCellStyleInfoIdentity = style.TableCellIdentity
    Dim cc As GridCurrentCell = e.TableControl.CurrentCell
    'Condition for checking the column header cell named 'Column4'
    If e.Inner.PopupCloseType = Syncfusion.Windows.Forms.PopupCloseType.Done AndAlso id.Column IsNot Nothing AndAlso id.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso id.Column.Name = "Column4" Then
       'check whether the current cell is combo box or not
       If TypeOf cc.Renderer Is GridComboBoxCellRenderer Then
           Dim rend As GridComboBoxCellRenderer = TryCast(cc.Renderer, GridComboBoxCellRenderer)
           choice_GGC = rend.ListBoxPart.SelectedItem.ToString()
           e.TableControl.CurrentCell.Refresh()
       End If
   End If
End Sub
'setting the styles, celltypes and cell value to the column header 4
Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs)
    'Condition for checking the column header cell named 'Column4'
    If e.TableCellIdentity.Column IsNot Nothing AndAlso e.TableCellIdentity.TableCellType = Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellType.ColumnHeaderCell AndAlso e.TableCellIdentity.Column.Name = "Column4" Then
        e.Style.Borders.All = border
        e.Style.BackColor = Color.White
        e.Style.CellType = "ComboBox"
        e.Style.ChoiceList = items
        e.Style.CellValue = choice_GGC
        e.Style.CellAppearance = GridCellAppearance.Flat
        e.Style.Enabled = True
    End If
End Sub
'Forcing the showing of popup while clicking on cell
Private Sub gridGroupingControl1_TableControlCellClick(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCellClickEventArgs)
    'Moving the clicked cell to current cell
    e.TableControl.CurrentCell.MoveTo(e.Inner.RowIndex, e.Inner.ColIndex)
    Dim style As GridTableCellStyleInfo = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex)
    Dim id As GridTableCellStyleInfoIdentity = style.TableCellIdentity
    'Condition for checking the column header cell named 'Column4'
    If id.Column IsNot Nothing AndAlso id.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso id.Column.Name = "Column4" AndAlso (Not e.TableControl.CurrentCell.IsDroppedDown) Then
        e.TableControl.CurrentCell.ShowDropDown() 'Forcing popup
    End If
End Sub

 

The following screenshot displays the ComboBox in a header cell.

Show combobox in a header cell

Figure 1: ComboBox in a header cell

Samples:

C#: ComboBox_in_header

VB: ComboBox_in_header


Conclusion

I hope you enjoyed learning about how to insert a ComboBox in a header cell of a grid in WinForms GridControl, GridGroupingControl and GridDataBoundGrid.

You can refer to our WinForms GridGrouping feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. 

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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