C1 FlexGrid for Winform的ComboBox列綁定與值

本文就來介紹,綁定模式下將值設置給C1FlexGrid的ComboBox列。在綁定FlexGrid的列展示IList類型

在當前的場景下,ComboBox的數據源是用戶定義的類,叫做CountryDetails。在grid中的每一行代表CountryDetail類的對象,然後類共有屬性通過columns(Country,Currency,Population等)計算。其中CountryDetails是列表:StateOptions(今天主要關注)在本文的示例中,我們允許用戶從數據源記錄相關中選擇state。

設置ContryDetails作為ComboBox控制項的數據源,然後保存選擇的值到對象的empty屬性。

工具/原料

ComponentOne
FlexGrid for Winform

方法/步驟

設置FlexGrid的數據源,然後創建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);

}

List循環計算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() { }

}

}

然後再第一列country中就會展示相關的值。

原作者:葡萄城控制項

本文內容整理自網絡, 文中所有觀點看法不代表淘大白的立場