今天在做系统时遇到一个问题,点击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;