我们可以给C1FlexGrid设置ComboBox列,那么如何在绑定模式下给每行设置不同的值?

本文就来介绍,绑定模式下将值设置给C1FlexGrid的ComboBox列。

在绑定FlexGrid的列展示IList类型

在当前的场景下,ComboBox的数据源是用户定义的类,叫做CountryDetails。在grid中的每一行代表CountryDetail类的对象,然后类共有属性通过columns(Country,Currency,Population等)计算。其中CountryDetails是列表:StateOptions(今天主要关注)

在本文的示例中,我们允许用户从数据源记录相关中选择state。

设置ContryDetails作为ComboBox控件的数据源,然后保存选择的值到对象的empty属性。

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.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中就会展示相关的值。

本文的示例请下载DynamicFlexGridC.zip (12.04 kb)

 

 

 更多资源

C1FlexGrid产品文档地址(中文): 

/docs/componentonestudio/flexgrid/

 

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

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