2017-02-16 65 views
0

通常我填comoboBox這樣:如何使用AutoComplete從CombBox中獲取SelectedValue?

combo.DataSource = dt; 
combo.ValueMember = "id"; 
combo.DisplayMember = "name"; 

,容易我可以選擇的值與此代碼

int i = convert.toint(combo.selectedValue); 

但我使用自動完成後,我無法得到它:

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"; 

現在如果我手動從組合框中選擇一個項目,我可以得到SelectedValue,但是如果我使用自動完成,我不能。當我從ComboBox中搜索Selected item時,我想獲取SelectedValue。

+0

是的,我這樣做,但在我的代碼,其我的寫錯誤只有 –

+0

*當*您檢查SelectedValue屬性? – LarsTech

+0

你嘗試過'comboSearch.Text'嗎? – NPras

回答

1

現在它做工精細

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; 




    } 

和的Form_Load我填寫組合框:

this.farmerNameCmb.DataSource = farmer.GET_FARMER_NAME_HAVE_PRODUCT(); 
      this.farmerNameCmb.DisplayMember = "farmerName"; 
      this.farmerNameCmb.ValueMember = "farmerID"; 

現在我可以在我的組合框搜索,並獲得ValuMember伊斯利

相關問題