[英]How to get SelectedValue from CombBox with AutoComplete?
Normally I fill comoboBox this way : 通常我以这种方式填充comoboBox:
combo.DataSource = dt;
combo.ValueMember = "id";
combo.DisplayMember = "name";
and easily I can get selected value with this code 我可以很容易地用此代码获得选定的值
int i = convert.toint(combo.selectedValue);
but after I use AutoComplete I can't get it: 但是在使用“自动完成”功能后,我无法获得它:
con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ACDB;Integrated Security=True;Pooling=False");
da = new SqlDataAdapter("SELECT id, name from usersTBL", con);
da.Fill(dt);
AutoCompleteStringCollection datasource = new AutoCompleteStringCollection();
for (int i = 0; i < dt.Rows.Count; i++)
{
datasource.Add(dt.Rows[i][1].ToString());
}
this.comboSearch.AutoCompleteCustomSource = datasource;
this.comboSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.comboSearch.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboSearch.DataSource = dt;
this.comboSearch.DisplayMember = "name";
this.comboSearch.ValueMember= "id";
Now if I select an item from the comboBox manually, I can get the SelectedValue, but if I use auto complete, I can't. 现在,如果我从comboBox中手动选择一个项目,则可以获取SelectedValue,但是如果我使用自动完成,则不能。 I want to get SelectedValue when I search for Selected item from the ComboBox.
我想从ComboBox中搜索Selected项目时获得SelectedValue。
now it work fine 现在工作正常
public frm_order()
{
InitializeComponent();
AutoCompleteStringCollection farmerdatasource = new AutoCompleteStringCollection();
for (int i = 0; i < farmer.GET_FARMER_NAME_HAVE_PRODUCT().Rows.Count;i++ ) {
farmerdatasource.Add(farmer.GET_FARMER_NAME_HAVE_PRODUCT().Rows[i][1].ToString());
}
this.farmerNameCmb.AutoCompleteCustomSource = farmerdatasource;
this.farmerNameCmb.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.farmerNameCmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
and in FORM_LOAD i fill combobox : 并在FORM_LOAD我填充组合框:
this.farmerNameCmb.DataSource = farmer.GET_FARMER_NAME_HAVE_PRODUCT();
this.farmerNameCmb.DisplayMember = "farmerName";
this.farmerNameCmb.ValueMember = "farmerID";
Now i can search in my comboBox and get the ValuMember easly 现在我可以在我的comboBox中搜索并轻松获取ValuMember
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.