Category / Section
How to change the ComboBox datasource based on the another cell value in WinForms GridControl and GridGroupingControl?
2 mins read
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.
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: