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;
}
} |
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?
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
Hi Tim Larson,
Currently we are analyzing the reported scenario, we will provide an update on or before October 24, 2023.
Regards,
Chidanand M.
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.
I have been able to get everything figured out and it is working great.