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

3 Replies

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

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 07:08 AM

Thank you that is exactly what I needed.!

Tim


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

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


Loader.
Live Chat Icon For mobile
Up arrow icon