C# WinForm 技巧二: Combobox手动绑定数据
2011-11-14 11:50
621 查看
定义一个类,有两个属性 /// <summary>
/// 元数据
/// </summary>
[System.Diagnostics.DebuggerStepThrough]
[Serializable]
public class SandData
{
String key = "";
Object value = "";
/// <summary>
/// 元数据
/// </summary>
public SandData() { }
/// <summary>
/// 元数据
/// </summary>
public SandData(String m_key,Object m_value) {
this.key = m_key;
this.value = m_value;
}
public String Key
{
get { return key; }
set { key = value; }
}
public Object Value
{
get { return this.value; }
set { this.value = value; }
} }
然后,用一个
ArrayList list = new ArrayList();
foreach (DataRow row in table.Rows)
{
string text = row["d_name"].ToString() +"[" + row["UptownName"].ToString() + row["BuildingName"].ToString() + row["UnitName"].ToString()+"]";
string value = row["Id"].ToString();
SandData vo = new SandData();
vo.Key = text;
vo.Value = value;
list.Add(vo);
}
this.comboBox1.DataSource = list;
this.comboBox1.DisplayMember = "Key";
this.comboBox1.ValueMember = "Value";
这样就OK了,取值只要cbb.SelectedValue就取到了。
/// 元数据
/// </summary>
[System.Diagnostics.DebuggerStepThrough]
[Serializable]
public class SandData
{
String key = "";
Object value = "";
/// <summary>
/// 元数据
/// </summary>
public SandData() { }
/// <summary>
/// 元数据
/// </summary>
public SandData(String m_key,Object m_value) {
this.key = m_key;
this.value = m_value;
}
public String Key
{
get { return key; }
set { key = value; }
}
public Object Value
{
get { return this.value; }
set { this.value = value; }
} }
然后,用一个
ArrayList list = new ArrayList();
foreach (DataRow row in table.Rows)
{
string text = row["d_name"].ToString() +"[" + row["UptownName"].ToString() + row["BuildingName"].ToString() + row["UnitName"].ToString()+"]";
string value = row["Id"].ToString();
SandData vo = new SandData();
vo.Key = text;
vo.Value = value;
list.Add(vo);
}
this.comboBox1.DataSource = list;
this.comboBox1.DisplayMember = "Key";
this.comboBox1.ValueMember = "Value";
这样就OK了,取值只要cbb.SelectedValue就取到了。
相关文章推荐
- C# Winform Combobox手动绑定数据
- c# winform comboBox动态绑定数据
- C# winform combobox 在绑定数据之后插入一项选择项
- C# winform combobox 在绑定数据之后插入一项选择项
- C# winform combobox 在绑定数据之后插入一项选择项
- C# [winform] combobox数据绑定,显示一个值,隐藏一个值
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法) .
- 转:c# winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法)
- C#_WinForm的ComboBox与数据绑定详解(关于Text属性和下拉列表)
- C# winform combobox 在绑定数据之后插入一项选择项
- winform技巧—combobox控件数据绑定
- C# winform combobox 在绑定数据之后插入一项选择项
- Winform开发之ComboBox和ComboBoxEdit控件绑定key/value数据
- 关于.net winform ComboBox数据绑定显示问题
- C#实现的多列数据绑定组合框控件MultiColumnComboBoxEx
- 如何为winform中的列表控件Combobox、ListBox控件绑定数据
- winform快速开发平台 -> 快速绑定ComboBox数据控件
- C# winform DATAGRIDVIEW 数据绑定
- C# WinForm 技巧:COMBOBOX搜索提示