1
我需要創建一個組合框自動完成這顯示text
Name
但是當我點擊text
它得到value
「ID
」與「名」的結合。我已經創建了一個代碼,但它不工作,我很困惑與set display text
和value
成組合框和自動完成data-source
綁定。C#組合框自動完成設置顯示文本和值
private void loadAutoCompleteValues()
{
autoCompleteCombo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
autoCompleteCombo.AutoCompleteSource = AutoCompleteSource.CustomSource;
DataTable products;
con.MysqlQuery("select * from products");
products = con.QueryEx();
Dictionary<string, string> comboSource = new Dictionary<string, string>();
for (int i = 0; i < products.Rows.Count; i++)
{
DataRow dr = products.Rows[i];
comboSource.Add(dr["id"].ToString(), dr["name"].ToString());
}
autoCompleteCombo.DataSource = new BindingSource(comboSource, null);
autoCompleteCombo.DisplayMember = "Value";
autoCompleteCombo.ValueMember = "Key";
}
private void autoCompleteCombo_SelectedIndexChanged(object sender, EventArgs e)
{
string key = ((KeyValuePair<string, string>)autoCompleteCombo.SelectedItem).Key;
string value = ((KeyValuePair<string, string>)autoCompleteCombo.SelectedItem).Value;
MessageBox.Show(key + " " + value);
}
你的意思是不工作?消息框正在顯示嗎? –
不,組合框是空的! – user3722956
它正在工作!我忘了'db con = new db();'''auto complete''不工作? – user3722956