← 返回所有博客文章

首先,我们来看下给FlexGrid for WinForms的ComboBox设置变量值。

如下图所示,我们需要给第一列产生新值。

DynamicFlexGrid

在FlexGrid绑定列展示IList类型字段

在这个示例中,我们的ComboBox的数据源是一个用户自定义列,叫做CountryDetail。

在表格中的每一行代表了CountryDetails类的对象,然后类公开属性通过列(Country, Currency, Population等)计算。

一个CountryDetails的属性是一个列表:StateOptions, 这个属性是我们今天要讲述的。

在示例中,用户可以选择和数据源相关的状态,这个界面控件是个ComboBox。

过程描述:设置CountryDetails作为ComboBox数据源,然后保存选择值到对象的空属性。

1.设置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);
}

 

2.循环计算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相关的值。

下载本文示例:

C#版本

VB版本

 

更多资源

C1FlexGrid for Winform中文帮助文档地址:

/docs/componentonestudio/flexgrid/

如果你对C1FlexGrid感兴趣,请到我们的官网下载最新版本:/download/?pid=3

如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-68.html