We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change the datasource for combobox cell dynamically in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl

Combobox customization

By default, particular cell style cannot be changed at run time for the GridGroupingControl without using QueryCellStyleInfo event. So, the datasource of the particular cell cannot be changed on run time.

In order to change the data source of the ComboBoxCell based on the text typed in the cell dynamically, custom cell renderer and cell model which is derived from ComboBoxCellRenderer and ComboBoxCellModel can be used.

Creating CellModel


public class MyAutocompleTextBoxCellModel : GridComboBoxCellModel 
    public MyAutocompleTextBoxCellModel(GridModel grid) 
        : base(grid) 
    /// Creates a cell renderer. 
    public override GridCellRendererBase CreateRenderer(GridControlBase control) 
        return new MyAutocompleteTextBoxCellRenderer(control, this); 



Public Class MyAutocompleTextBoxCellModel
    Inherits GridComboBoxCellModel
    Public Sub New(ByVal grid As GridModel)
    End Sub
    ''' Creates a cell renderer. 
    Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase
        Return New MyAutocompleteTextBoxCellRenderer(control, Me)
    End Function
End Class


Creating CellRenderer


public class MyAutocompleteTextBoxCellRenderer : GridComboBoxCellRenderer
    GridStyleInfo cellStyle = null;
    public MyAutocompleteTextBoxCellRenderer(GridControlBase grid, GridCellModelBase cellModel)
        : base(grid, cellModel)
        cellStyle = GridStyleInfo.Default;
    Hashtable styleCollection = new Hashtable();
    protected override void OnSetControlText(string text)
        if (text == "Desc6")
            if (!styleCollection.ContainsKey(GridRangeInfo.Cell(this.RowIndex, this.ColIndex)))
                GridStyleInfo style = GridStyleInfo.Empty;
                DataTable table = new DataTable();
                table.Columns.Add("Desc ID");
                table.Rows.Add("Desc1", 1);
                table.Rows.Add("Desc2", 2);
                table.Rows.Add("Desc3", 3);
                style.DataSource = table;
                style.DisplayMember = "Description";
                style.ValueMember = "Desc ID";
                styleCollection.Add(GridRangeInfo.Cell(this.RowIndex, this.ColIndex), style);
    protected override void OnKeyPress(KeyPressEventArgs e)
    public override void OnPrepareViewStyleInfo(GridPrepareViewStyleInfoEventArgs e)
        if (styleCollection.Contains(GridRangeInfo.Cell(e.RowIndex, e.ColIndex)))
            GridStyleInfo modifiedStyle = ((GridStyleInfo)styleCollection[GridRangeInfo.Cell(e.RowIndex, e.ColIndex)]);
            e.Style.DataSource = modifiedStyle.DataSource;
            e.Style.DisplayMember = modifiedStyle.DisplayMember;
            e.Style.ValueMember = modifiedStyle.ValueMember;



Public Class MyAutocompleteTextBoxCellRenderer
    Inherits GridComboBoxCellRenderer
    Private cellStyle As GridStyleInfo = Nothing
    Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase)
        MyBase.New(grid, cellModel)
        cellStyle = GridStyleInfo.Default
    End Sub
    Private styleCollection As New Hashtable()
    Protected Overrides Sub OnSetControlText(ByVal text As String)
        If text = "Desc6" Then
            If Not styleCollection.ContainsKey(GridRangeInfo.Cell(Me.RowIndex, Me.ColIndex)) Then
                Dim style As GridStyleInfo = GridStyleInfo.Empty
                Dim table As New DataTable()
                table.Columns.Add("Desc ID")
                table.Rows.Add("Desc1", 1)
                table.Rows.Add("Desc2", 2)
                table.Rows.Add("Desc3", 3)
                style.DataSource = table
                style.DisplayMember = "Description"
                style.ValueMember = "Desc ID"
                styleCollection.Add(GridRangeInfo.Cell(Me.RowIndex, Me.ColIndex), style)
            End If
        End If
    End Sub
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
    End Sub
    Public Overrides Sub OnPrepareViewStyleInfo(ByVal e As GridPrepareViewStyleInfoEventArgs)
        If styleCollection.Contains(GridRangeInfo.Cell(e.RowIndex, e.ColIndex)) Then
            Dim modifiedStyle As GridStyleInfo = (CType(styleCollection(GridRangeInfo.Cell(e.RowIndex, e.ColIndex)), GridStyleInfo))
            e.Style.DataSource = modifiedStyle.DataSource
            e.Style.DisplayMember = modifiedStyle.DisplayMember
            e.Style.ValueMember = modifiedStyle.ValueMember
        End If
    End Sub
End Class


Adding CellModel and Assigning CellType


//Adding CellModel
this.gridGroupingControl1.TableModel.CellModels.Add("AutocompleteText", newMyAutocompleTextBoxCellModel(this.gridGroupingControl1.TableModel)); 
//Assigning CellType
this.gridGroupingControl1.TableDescriptor.Columns["Description"].Appearance.AnyRecordFieldCell.CellType = "AutocompleteText";



'Adding CellModel
Me.gridGroupingControl1.TableModel.CellModels.Add("AutocompleteText", newMyAutocompleTextBoxCellModel(Me.gridGroupingControl1.TableModel))
'Assigning CellType
Me.gridGroupingControl1.TableDescriptor.Columns("Description").Appearance.AnyRecordFieldCell.CellType = "AutocompleteText"



Customize the combobox in GridGroupingControl

Customize the combobox in GridGroupingControl



C#: ComboBox Cell Datasource CS

VB: ComboBox Cell Datasource VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile