0

This is my function that is loading some values to a comboBox:

void comboFill()
{
    try
    {
        string connectionString = "Data Source=D\\SQLEXPRESS;Initial Catalog=Inventory;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        string query = "SELECT * FROM dbo.Inven; ";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string invDes = dr.GetString(dr.GetOrdinal("Description"));
            comboBox1.Items.Add(invDes);
        }
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

How would I use "contains" to check for a character and then display the comboBox while I type? For eg. the comboBox would have the items: cat, bird, billy. And when I type the letter "i" it brings up "bird" and "billy" in the dropdown of values. I know it is something like this:

if (comboBox1.Items.Contains("some value")){}

But am not entirely sure how to implement it in this case. To clarify I do not want an autocomplete comboBox as this does not take into account substring searches.

AJ26
  • 501
  • 4
  • 15
  • also refactor your Sql Objects to use the `using(){}` construct as well if you are only filling the ComboBox with `Description` I would highly suggest that you change the sql query to `Select Description from dbo.Inven` – MethodMan Dec 13 '16 at 19:34
  • This is not what i want. This only accounts for the first letter. I also want it for substrings. For eg. If I type in a letter other than the first one. I already am aware of the autocomplete – AJ26 Dec 13 '16 at 19:35
  • then you can change your combobox to bind to a DataTable as well as taking advantage of the DataView and it's RowFilter Property do a google search on how to Filter DataTable you could still bind the single column itself to whatever you're filtering on ..very simple actually – MethodMan Dec 13 '16 at 19:38

0 Answers0