0
Reply

how to search like google list

sunny saharawat

sunny saharawat

May 2 2013 8:09 AM
1.7k
i want to search data table on textbox as if i press "s" in textbox it show a list of all name start with s and if add "u" as "su" it show all name in a list and
on selecting a name list disappeare  it sound something like google..................here are my code......... on text change event.......

or it show in run time generated combobox as item and remove on item selecting or empty text box here it is not removing.........
pls tell me where r the error  or possibility.........
 //search
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
          if(textBox1.Text==string.Empty)
          {
              ComboBox cmb = new ComboBox();
             // this.Controls.Remove(cmb);
              cmb.Visible = false;
              con = new SqlConnection(conStr);
              con.Open();
              query = "select * from Item_setup";
              da = new SqlDataAdapter();
              da.SelectCommand = new SqlCommand(query, con);
              dt = new DataTable();
              da.Fill(dt);
              dataGridView1.DataSource = dt;
              con.Close();
              listBox1.DataSource = null;
              comboBox1.DataSource = null;
              cmb.DataSource = null;
              this.Controls.Remove(cmb);
          }
          else
          {
              ComboBox cmb = new ComboBox();
              string searchFor = "%" + textBox1.Text + "%";
              SqlConnection sqlConn = new SqlConnection("Data Source=.;Initial Catalog=Inventry;Integrated Security=True");
              string sqlQuery = @"SELECT  distinct(display_name) AS display_name
                    FROM item_setup
                    WHERE display_name Like @name";
              SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
              cmd.Parameters.AddWithValue("@name", searchFor);
              DataTable table = new DataTable("item_setup");
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(table);
              cmb.Size = new Size(200, 50);
              cmb.Location = new Point(220,80);
              cmb.DataSource = new BindingSource(table, null);
              this.Controls.Add(cmb);
              comboBox1.DataSource = new BindingSource(table, null);
              listBox1.DataSource = new BindingSource(table, null);
              dataGridView1.DataSource = new BindingSource(table, null);
          }