Let's look at how you can assign a variable set of values to a ComboBox in FlexGrid for WinForms. After this quick tutorial you'll be able to bind your FlexGrid to up to two tiers of IList data sources. As you can see in the image below, we need to generate a new set of provinces according to the value in the first column. DynamicFlexGrid

Displaying an IList type field in column of bound FlexGrid

In the current scenario, our data source for the ComboBox is a user-defined class named CountryDetails. Each row in the grid represents one object of the CountryDetails class, and the class's public properties are populated across the columns (Country, Currency, Population, etc.). One of the properties of CountryDetails is a List: StateOptions, and this is the property we'll be focusing on today. In this sample we'll allow the user to select which state should be associated with the record in the data source; the UI control will be a ComboBox. The process is straightforward: set CountryDetails as the data source of the ComboBox control, and save the selected value into the object's empty property.

1. Set the Data Source of FlexGrid and create the lists of provinces.


namespace DynamicFlexGrid  
{  
public partial class Form1 : Form  
{  
List objectList;  

public Form1()  
{  
InitializeComponent();  
}  

private void Form1_Load(object sender, EventArgs e)  
{  
objectList = setupMyBusinessObject();  

c1FlexGrid1.DataSource = objectList;  

c1FlexGrid1.AutoSizeCols();  
c1FlexGrid1.Cols[4].AllowEditing = false;  
c1FlexGrid1.Cols[2].Caption = "City/State";  
}  

List setupMyBusinessObject()  
{  
List mboList = new List();  
CountryDetails mbo = new CountryDetails();  
mbo.Country = "Afghanistan";  
mbo.Currency = "Afghan afghani";  
mbo.Population = "30.55 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Mazar-i-Sharif");  
mbo.StateOptions.Add("Kabul");  
mbo.StateOptions.Add("Kandahar");  
mbo.StateOptions.Add("Herat");  
mbo.StateOptions.Add("Kunduz");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Belgium";  
mbo.Currency = "Euro";  
mbo.Population = "11.2 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Antwerp");  
mbo.StateOptions.Add("East Flanders");  
mbo.StateOptions.Add("Flemish Brabant");  
mbo.StateOptions.Add("Flemish Brabant");  
mbo.StateOptions.Add("West Flanders");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Canada";  
mbo.Currency = "Canadian dollar";  
mbo.Population = "35.16 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Ontario");  
mbo.StateOptions.Add("Quebec");  
mbo.StateOptions.Add("Nova Scotia");  
mbo.StateOptions.Add("New Brunswick");  
mbo.StateOptions.Add("Manitoba");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Denmark";  
mbo.Currency = "Danish krone";  
mbo.Population = "5.614 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Copenhagen");  
mbo.StateOptions.Add("Aarhus");  
mbo.StateOptions.Add("Odense");  
mbo.StateOptions.Add("Aalborg");  
mbo.StateOptions.Add("Frederiksberg");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Egypt";  
mbo.Currency = "Egyptian pound";  
mbo.Population = "82.06 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Cairo");  
mbo.StateOptions.Add("Alexandria");  
mbo.StateOptions.Add("Giza");  
mbo.StateOptions.Add("Shubra El-Kheima");  
mbo.StateOptions.Add("Port Said");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "France";  
mbo.Currency = "Euro";  
mbo.Population = "66.03 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Alsace");  
mbo.StateOptions.Add("Aquitaine");  
mbo.StateOptions.Add("Auvergne");  
mbo.StateOptions.Add("Brittany");  
mbo.StateOptions.Add("Burgundy");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Germany";  
mbo.Currency = "Euro";  
mbo.Population = "80.62 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Baden-W√ľrttemberg");  
mbo.StateOptions.Add("Bavaria");  
mbo.StateOptions.Add("Berlin");  
mbo.StateOptions.Add("Brandenburg");  
mbo.StateOptions.Add("Bremen");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Hungary";  
mbo.Currency = "Hungarian forint";  
mbo.Population = "9.897 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Budapest");  
mbo.StateOptions.Add("Debrecen");  
mbo.StateOptions.Add("Miskolc");  
mbo.StateOptions.Add("Szeged");  
mbo.StateOptions.Add("Szombathely");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "India";  
mbo.Currency = "Rupee";  
mbo.Population = "1.252 billion";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Uttar Pradesh");  
mbo.StateOptions.Add("Madhya Pradesh");  
mbo.StateOptions.Add("Maharashtra");  
mbo.StateOptions.Add("Shimla");  
mbo.StateOptions.Add("Goa");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Japan";  
mbo.Currency = "Japanese yen";  
mbo.Population = "127.3 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Nagoya");  
mbo.StateOptions.Add("Tokoname");  
mbo.StateOptions.Add("Nisshin");  
mbo.StateOptions.Add("Utashinai");  
mbo.StateOptions.Add("Sendai");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Kenya";  
mbo.Currency = "Kenyan shilling";  
mbo.Population = "44.35 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Mombasa");  
mbo.StateOptions.Add("Nairobi");  
mbo.StateOptions.Add("Kisumu");  
mbo.StateOptions.Add("Eldoret");  
mbo.StateOptions.Add("Lodwar");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Libya";  
mbo.Currency = "Libyan dinar";  
mbo.Population = "6.202 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Nuqat al Khams");  
mbo.StateOptions.Add("Zawiya");  
mbo.StateOptions.Add("Jafara");  
mbo.StateOptions.Add("Tripoli");  
mbo.StateOptions.Add("Murqub");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Madagascar";  
mbo.Currency = "Malagasy ariary";  
mbo.Population = "22.92 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Diana");  
mbo.StateOptions.Add("Sava");  
mbo.StateOptions.Add("Itasy");  
mbo.StateOptions.Add("Analamanga");  
mbo.StateOptions.Add("Vakinankaratra");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "New Zealand";  
mbo.Currency = "New Zealand dollar";  
mbo.Population = "4.471 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Auckland");  
mbo.StateOptions.Add("Wellington");  
mbo.StateOptions.Add("Christchurch");  
mbo.StateOptions.Add("Hamilton");  
mbo.StateOptions.Add("Napier-Hastings");  
mboList.Add(mbo);  

mbo = new CountryDetails();  
mbo.Country = "Oman";  
mbo.Currency = "Omani rial";  
mbo.Population = "3.632 million";  
mbo.StateOptions = new List();  
mbo.StateOptions.Add("Ad Dakhiliyah");  
mbo.StateOptions.Add("Ad Dhahirah North");  
mbo.StateOptions.Add("Al Batinah North");  
mbo.StateOptions.Add("Al Batinah South");  
mbo.StateOptions.Add("Al Buraimi");  
mboList.Add(mbo);  

return (mboList);  
}  

2. Loop through the List to populate the values of the ComboBox.


private void c1FlexGrid1_BeforeEdit(object sender, RowColEventArgs e)  
{  
if (e.Col == 2)  
{  
comboBox1.DataSource = objectList[e.Row - 1].StateOptions;  
c1FlexGrid1.Cols[2].Editor = comboBox1;  
}  
}  

private void c1FlexGrid1_CellChanged(object sender, RowColEventArgs e)  
{  
c1FlexGrid1.AutoSizeCols();  
}  
}  

public class CountryDetails  
{  
public string Country { get; set; }  
public string States { get; set; }  
public List StateOptions { get; set; }  
public string Population { get; set; }  
public string Currency { get; set; }  

public CountryDetails() { }  
}  
}  

Each row will display the values concurrent with the first column's country.