Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search in a combo box

I have a combo box called cbProduit; the combo box is being filled via a web service:

ComboBoxItemProduit produiItem = new ComboBoxItemProduit();
produiItem.Text = articleArray.GetAllArticlesResult[i].S_MODELE;
produiItem.Value = articleArray.GetAllArticlesResult[i].S_ID;
cbProduit.Items.Add(produiItem);

The problem is, the combo box, when it is filled, contains more than 30000 items and I need to make a search by text.

Note: I don't have any relation with a database all the info came from a Web Service.

Can anyone help, please?

like image 412
Abdellah Brashe Avatar asked Nov 17 '25 16:11

Abdellah Brashe


1 Answers

there are two options I can see that fits your description.

option 1:

you set an autocomplete property for the combobox like this:

comboBox1.DataSource = dt;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "VALUE";
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

but then, it would work only on your first character and will show a list like this: enter image description here

option 2:

putting a new text box with a textChanged event:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox1.Text))
    {
        comboBox1.DataSource = dt; //your origin data

    }
    else
    {
        var newTable = dt.AsEnumerable()
      .Where(x => x.Field<string>("VALUE").ToUpper().Contains(textBox1.Text.ToUpper()))
      .CopyToDataTable();

        comboBox1.DataSource = newTable;

    }
}

while dt is your origin data that came from the server and the result is this: enter image description here

like image 65
Dor Lugasi-Gal Avatar answered Nov 20 '25 06:11

Dor Lugasi-Gal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!