您的位置:首页 > 产品设计 > UI/UE

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;
本文出自 “初来咋到” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: