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

Cascading Combobox sample needed

I am trying to use the sfDataGrid and create a simple combobox of Country the user will select the country and then the next combobox will be a dropdown of the states for each country.
I am tring to populate the combox values from a datatable and I keep getting the key already exists.

Please provide a simple example of above using a datatable.

Thanks,
Tim

8 Replies

JN Jayaleshwari N Syncfusion Team December 14, 2018 11:57 AM UTC

Hi Tim, 
 
Thanks for using Syncfusion Products. 
 
We have checked the reported query “Change the items source of combo box column based on another combo box column value” from our side. You can achieve your requirement by using IDataSourceSelector property of the GridComboBoxColumn.  
 
Code Example : IDataSourceSelector implementation and assigned to GridComboBoxColumn. 
  
public Form1() 
{ 
    InitializeComponent(); 
    this.sfDataGrid.Columns.Add(new GridComboBoxColumn() { MappingName = "State", DisplayMember = "State", ValueMember = "State", IDataSourceSelector = new CustomSelector() }); 
} 
 
public class CustomSelector : IDataSourceSelector 
{ 
    DataTable statesOfUS; 
    DataTable statesOfBrazil; 
 
    public CustomSelector() 
    { 
        statesOfBrazil = GetStatesOfBrazil(); 
        statesOfUS = GetStatesOfUS(); 
    } 
 
    public IEnumerable GetDataSource(object record, object dataSource) 
    { 
        if (record == null) 
            return null; 
 
        var data = record as DataRowView; 
        var countryName = data.Row["Country"].ToString(); 
 
        if (countryName == "US") 
            return statesOfUS.DefaultView; 
        else if (countryName == "Brazil") 
            return statesOfBrazil.DefaultView; 
 
        return null; 
    } 
 
    public DataTable GetStatesOfUS() 
    { 
        DataTable collection = new DataTable(); 
        collection.Columns.Add("State", typeof(string)); 
        collection.Rows.Add("Alaska"); 
        collection.Rows.Add("California"); 
        collection.Rows.Add("Colorado"); 
 
        return collection; 
    } 
 
    public DataTable GetStatesOfBrazil() 
    { 
        DataTable collection = new DataTable(); 
        collection.Columns.Add("State", typeof(string)); 
        collection.Rows.Add("Rio de Janeiro"); 
        collection.Rows.Add("Bahia"); 
        collection.Rows.Add("Roraima"); 
 
        return collection; 
    } 
} 
 
We have attached the sample for your reference and you can download the same from the following location. 
 
Please let us know if you would require further assistance. 
 
 
Regards, 
Jayaleshwari N 



TL Tim Larson December 14, 2018 12:08 PM UTC

Thank you that is exactly what I needed.!

Tim


JN Jayaleshwari N Syncfusion Team December 17, 2018 07:22 AM UTC

Hi Tim, 
 
Thanks for the update. Please get in touch if you would require further assistance. 
 
Regards, 
Jayaleshwari N 



DF Duane Flanders October 18, 2023 05:36 PM UTC

I have tried using your example and it fails with "Column State does not belong to table." This is happening when the country changes. I am using VS 2022 with .NET Framework 4.8. I installed both VS 2017 and .NET Framework 4.6 to test and get the same error. Any Ideas?



SG Santhosh Govindasamy Syncfusion Team October 19, 2023 03:00 PM UTC

Hi Duane,

We are currently analyzing your reported scenario. We need time to validate the scenario, we will provide an update on or before October 24, 2023.


Regards,
Santhosh.G



CM Chidanand Murugaiah Syncfusion Team October 19, 2023 07:31 PM UTC

Hi Tim Larson,


Currently we are analyzing the reported scenario, we will provide an update on or before October 24, 2023.


Regards,

Chidanand M.



DF Duane Flanders October 19, 2023 08:48 PM UTC

I have got it working by modifying the sample DataSourceSeletorinComboBox168358430. Is it possible to have it work when adding a new row? I will continue to work on it from my end.



DF Duane Flanders October 21, 2023 06:03 PM UTC

I have been able to get everything figured out and it is working great. 


Loader.
Live Chat Icon For mobile
Up arrow icon