How to change the ComboBox datasource in WinForms GridControl?
Change the combobox datasource based on specific cell value
To modify the data source of combo box cells based on a specific cell value, use the e.Style.DataSource property of the QueryCellStyleInfo event in WinForms Grid Control.
GridControl
C#
this.gridControl1.QueryCellInfo += GridControl1_QueryCellInfo; private void GridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) { if (e.RowIndex > 0 && e.ColIndex == 5) { GridStyleInfoStore data = this.gridControl1.Data[e.RowIndex, e.ColIndex - 1] as GridStyleInfoStore; if (data != null) { GridStyleInfo style = new GridStyleInfo(data); switch (style.Text) { case "Australia": e.Style.DataSource = ausStates; break; case "Canada": e.Style.DataSource = canadaStates; break; case "France": e.Style.DataSource = franceStates; break; case "Germany": e.Style.DataSource = germanyStates; break; case "United Kingdom": e.Style.DataSource = ukStates; break; case "United States": e.Style.DataSource = ussStates; break; } } } }
VB
AddHandler Me.gridControl1.QueryCellInfo, AddressOf GridControl1_QueryCellInfo Private Sub GridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) If e.RowIndex > 0 AndAlso e.ColIndex = 5 Then Dim data As GridStyleInfoStore = TryCast(Me.gridControl1.Data(e.RowIndex, e.ColIndex - 1), GridStyleInfoStore) If data IsNot Nothing Then Dim style As New GridStyleInfo(data) Select Case style.Text Case "Australia" e.Style.DataSource = ausStates Case "Canada" e.Style.DataSource = canadaStates Case "France" e.Style.DataSource = franceStates Case "Germany" e.Style.DataSource = germanyStates Case "United Kingdom" e.Style.DataSource = ukStates Case "United States" e.Style.DataSource = ussStates End Select End If End If End Sub
Samples
C# : GridControl_CS
VB: GridControl_VB
GridGroupingControl
C#
this.gridGroupingControl1.QueryCellStyleInfo += gridGroupingControl1_QueryCellStyleInfo; void gridGroupingControl1_QueryCellStyleInfo(object sender,GridTableCellStyleInfoEventArgs e) { if (e.TableCellIdentity == null || e.TableCellIdentity.Column == null) return; Element element = e.TableCellIdentity.DisplayElement; if (element != null && element.Kind == DisplayElementKind.AddNewRecord) { if (e.TableCellIdentity.Column.Name == "CategoryID") { e.Style.CellType = GridCellTypeName.ComboBox; e.Style.DataSource = id; } else if (e.TableCellIdentity.Column.Name == "SampleData") { e.Style.CellType = GridCellTypeName.ComboBox; Record record = el.GetRecord(); //Get the first column cell value var value = record.GetValue("CategoryID"); //Get the collection based on the first column value if (value != null &&value.ToString() != string.Empty) { List<CasCading> source = cascadingSource[value.ToString()]; //Assign the collection for second column. e.Style.DataSource = source; e.Style.DisplayMember = " SampleData"; } } } }
VB
AddHandler Me.gridGroupingControl1.QueryCellStyleInfo, AddressOf gridGroupingControl1_QueryCellStyleInfo Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As GridTableCellStyleInfoEventArgs) If e.TableCellIdentity Is Nothing OrElse e.TableCellIdentity.Column Is Nothing Then Return End If Dim element As Element = e.TableCellIdentity.DisplayElement If element IsNot Nothing AndAlso element.Kind = DisplayElementKind.AddNewRecord Then If e.TableCellIdentity.Column.Name = "CategoryID" Then e.Style.CellType = GridCellTypeName.ComboBox e.Style.DataSource = id ElseIf e.TableCellIdentity.Column.Name = "SampleData" Then e.Style.CellType = GridCellTypeName.ComboBox Dim record As Record = el.GetRecord() 'Get the first column cell value Dim value = record.GetValue("CategoryID") 'Get the collection based on the first column value If value IsNot Nothing AndAlso value.ToString() <> String.Empty Then Dim source As List(Of CasCading) = cascadingSource(value.ToString()) 'Assign the collection for second column. e.Style.DataSource = source e.Style.DisplayMember = " SampleData" End If End If End If End Sub
Samples:
I hope you enjoyed learning about how to change the ComboBox datasource in WinForms GridControl.
You can refer to our WinForms Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!