简体   繁体   中英

How to get SelectedValue from CombBox with AutoComplete?

Normally I fill comoboBox this way :

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. I want to get SelectedValue when I search for Selected item from the ComboBox.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM