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 ComboBox datasource based on the another cell value in WinForms GridControl and GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: September 21, 2018 |
Last Revised Date: February 11, 2020
Tags: combobox

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:

C#: GridGroupingControl_CS

VB: GridGroupingControl_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