)
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. (Last updated on: June 24, 2019).
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 |
Published Date: June 29, 2016 |
Last Revised Date: December 24, 2019

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

C#

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); 
    } 
} 

 

VB

Public Class MyAutocompleTextBoxCellModel
    Inherits GridComboBoxCellModel
    Public Sub New(ByVal grid As GridModel)
        MyBase.New(grid)
    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

C#

public class MyAutocompleteTextBoxCellRenderer : GridComboBoxCellRenderer
{
    GridStyleInfo cellStyle = null;
    public MyAutocompleteTextBoxCellRenderer(GridControlBase grid, GridCellModelBase cellModel)
        : base(grid, cellModel)
    {
        this.RemoveButton(this.DropDownButton);
        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;
                style.CopyFrom(this.StyleInfo);
                DataTable table = new DataTable();
                table.Columns.Add("Description");
                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);
            }
        }
        base.OnSetControlText(text);
    }
 
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        this.Grid.CurrentCell.ShowDropDown();
        base.OnKeyPress(e);
    }
 
    public override void OnPrepareViewStyleInfo(GridPrepareViewStyleInfoEventArgs e)
    {
        base.OnPrepareViewStyleInfo(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;
        }
    }
}

 

VB

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)
        Me.RemoveButton(Me.DropDownButton)
        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
                style.CopyFrom(Me.StyleInfo)
                Dim table As New DataTable()
                table.Columns.Add("Description")
                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
        MyBase.OnSetControlText(text)
    End Sub
 
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        Me.Grid.CurrentCell.ShowDropDown()
        MyBase.OnKeyPress(e)
    End Sub
 
    Public Overrides Sub OnPrepareViewStyleInfo(ByVal e As GridPrepareViewStyleInfoEventArgs)
        MyBase.OnPrepareViewStyleInfo(e)
        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

C#

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

 

VB

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

 

Screenshot

Customize the combobox in GridGroupingControl

Customize the combobox in GridGroupingControl

 

Samples:

C#: ComboBox Cell Datasource CS

VB: ComboBox Cell Datasource VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
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
Live Chat Icon