Pravin Ghadge

Pravin Ghadge

  • 563
  • 2.1k
  • 586.7k

Autocomplete in combobox of datagridview

Oct 28 2011 12:00 AM
Hi

I have gridview with 2 column (Group Name,ProductName).
I have used following code for autocomplete bt it doesnt work:

At Form Load:
setAutoComplete();

     private void setAutoComplete()
        {
            try
            {
                SqlDataReader dReader;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = oDAL.OpenConnection();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select PNAME from tbl_Master_Product ";
                dReader = cmd.ExecuteReader();
                if (dReader.HasRows == true)
                {
                    while (dReader.Read())
                        namesCollection.Add(dReader[0].ToString());


                }
                dReader.Close();
                oDAL.CloseConnection();
                cmd.Dispose();
            }
            catch (Exception EX)
            { 
           
            }
        }
 AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
        private void DGVDetails_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {
                if (e.Control is DataGridViewComboBoxEditingControl)
                {
                
                        if (DGVDetails.CurrentCell.ColumnIndex == 5)
                        {                  


                            ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                            ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
                           ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.CustomSource;
                       
                            ((ComboBox)e.Control).AutoCompleteCustomSource = namesCollection;
                
                        }
                        else
                        {
                            prodCode.AutoCompleteMode = AutoCompleteMode.None;
                        }
                 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

 
It is working properly when iam using textbox in place of comboobx.

& my 2nd question is how should i display respective product when i click group name.
For eg:
If i click Nokia in group name then in Product name combobox column only nokia products should be diplayed which are stored at group master table








Answers (4)