How to change the datasource for ComboBox cell dynamically in WinForms GridGroupingControl?
ComboBox customization
By default, a particular cell style cannot be changed at run time for the WinForms GridGroupingControl without using the QueryCellStyleInfo event. So, the datasource of the
particular cell cannot be changed on runtime.
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
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;
}
}
}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
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" The screenshot below illustrates the dynamic change of the data source for a combobox cell.


Samples:
C#: ComboBox Cell Datasource CS
VB: ComboBox Cell Datasource VB
Conclusion
I hope you
enjoyed learning about how to change the datasource for combobox cell
dynamically in WinForms GridGroupingControl.
You can refer to our WinForms GridGroupingControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms 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 WinForms GridGroupingControl and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!