Click here to Skip to main content
14,388,768 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to use AutoComplete Function My Values Are Comming from SQL Database
For Example, If I Search Keyboard I Write K It Shows On Drop Down List KeyBoard But IF I
Search e or Y or any other Letter It Doesn't Show Keyboard

What I have tried:

public partial class Form1 : Form
    {
        private string connStr = @"Data Source=.;Initial Catalog=ranfttech_pos;Persist Security Info=True;User ID=sa;Password=Nid@@sad;";
        DataTable dt;   

        public Form1()
        {
            InitializeComponent();

        }
        ranfttech_posDataSet db = new ranfttech_posDataSet();
        
        private void Form1_Load(object sender, EventArgs e)
        {
            {
                Configure_ComboBox();
            }
    void Configure_ComboBox()
       {
                this.Connect();
                if (dt == null)
                {
                    MessageBox.Show("Error in Quering");
                    return;
                }
                IList<string> lstFirst = new List<string>();
               
                foreach (DataRow row in dt.Rows)
                {
                    lstFirst.Add(row.Field<string>("name"));
                   
                }
                this.comboBox1.Items.AddRange(lstFirst.ToArray<string>());
                this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
               
            }
           
        }

        private void Connect()
        {
            SqlConnection conn = new SqlConnection(this.connStr);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(@"select name from tbl_product where name LIKE '%%'", conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                dt = new DataTable();
                ada.Fill(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message.ToString());
            }
            finally { conn.Close(); }
        }
Posted
Updated 6-Sep-19 21:20pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

See answer here: C# winforms combobox dynamic autocomplete - Stack Overflow[^]
And here: Autocomplete in a ComboBox Using C# and SQL Server[^]

You can also set:
this.comboBox1.DroppedDown = false;
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100