C# combobox 添加key和value
2012-06-26 17:36
316 查看
今天在做系统时遇到一个问题,点击dataGridView某一行时,要在上面的textbox和ComboBox中有显示相应的数据。由于数据库中存的只是combobox中items的id值,因此在ComboBox的数据显示时,出现了苦难。后来经过多次尝试终于找到了一个比较理想的解决方案,现在分享给大家。
public class ComboBoxItem
{
private string _key = string.Empty;
private string _value = string.Empty;
public ComboBoxItem(string pKey, string pValue)
{
_key = pKey;
_value = pValue;
}
public override string ToString()
{
return this._value;
}
public string Key
{
get
{
return this._key;
}
set
{
this._key = value;
}
}
public string Value
{
get
{
return this._value;
}
set
{
this._value = value;
}
}
public static int getIndex(ComboBox aa, string a)
{
ArrayList al = new ArrayList();
for (int i = 0; i < aa.Items.Count;i++ )
{
al.Add(((ComboBoxItem)aa.Items[i]).Key);
}
return al.IndexOf(a);
int s = ConstDataCtl.getIndex(this.comboBox2, this.dataGridView1.SelectedRows[0].Cells[8].Value.ToString());
this.comboBox2.SelectedIndex = s;
本文出自 “初来咋到” 博客,转载请与作者联系!
public class ComboBoxItem
{
private string _key = string.Empty;
private string _value = string.Empty;
public ComboBoxItem(string pKey, string pValue)
{
_key = pKey;
_value = pValue;
}
public override string ToString()
{
return this._value;
}
public string Key
{
get
{
return this._key;
}
set
{
this._key = value;
}
}
public string Value
{
get
{
return this._value;
}
set
{
this._value = value;
}
}
public static int getIndex(ComboBox aa, string a)
{
ArrayList al = new ArrayList();
for (int i = 0; i < aa.Items.Count;i++ )
{
al.Add(((ComboBoxItem)aa.Items[i]).Key);
}
return al.IndexOf(a);
int s = ConstDataCtl.getIndex(this.comboBox2, this.dataGridView1.SelectedRows[0].Cells[8].Value.ToString());
this.comboBox2.SelectedIndex = s;
本文出自 “初来咋到” 博客,转载请与作者联系!
相关文章推荐
- C# 使用 DictionaryEntry 在WinForm中给 ComboBox 添加 Key 和 Value
- C#中如何为ComboBox添加key-value对
- C#中如何为ComboBox添加key-value对
- c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key
- WinForm中ComboBox添加Key/Value项、获取选中项、根据Key、Value设置选中项总结
- [原创]C# ComboBox绑定key和value
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决
- C#中ComboBox和ListBox添加项完全解决 - 转
- C# Dictionary的用法 KeyValuePair<TKey, TValue> 结构
- 2014-3-28-C#正则表达式-传入key获取value
- 【iOS开发-60】案例学习:多组数据的tableView设置、添加右側组索引、多层数据模型设置以及valueForKeyPath
- [转]c#(winform) 中 ComboBox ListBox 添加项完全解决
- c#(winform)中ComboBox和ListBox添加项完全解决
- C# 通过JSON 键(key)找值(value)
- C#学习常用类(1002)---KeyValuePair<TKey, TValue> 结构
- C# Dictionary已知value获取对应的key
- comboBox绑定key和value
- C# ComboBox(DropDownList)数据绑定再添加一项
- C# 键盘中的按键对应的KeyValue
- C#中Combobox中添加Item项问题