You must Sign In to post a response.
  • Category: .NET

    How to create autocomplete for combobox

    I am create windows application using c# 2010, I am using data grid view for this application in my grid view 2 columns, I am using combo box but how to create autocomplete for my combo boxes.

    Give me any one ideas.
  • #766220
    You need to set the Auto Complete mode of the combo box & set a datasource for it to fetch data from.

    Steps:

    Step 1: Set AutoCompleteMode to Suggest or SuggestAppend. Just Suggest works fine.

    cbAC.AutoCompleteMode = AutoCompleteMode.Suggest;

    Step 2: Set AutoCompleteSource to a CustomSource.

    cbAC.AutoCompleteSource = AutoCompleteSource.CustomSource;

    Step 3: Create a AutoCompleteStringCollection.

    AutoCompleteStringCollection cbCollect = new AutoCompleteStringCollection();

    Step 4: Fetch your data from database or whichever source you wish.

    fetchData(cbCollect); //Return a AutoCompleteStringCollection object from the method.

    Step 5: Assign the fetched data as datasource to the combo box's AutoCompleteCustomSource property.

    cbAC.AutoCompleteCustomSource = cbCollect;

  • #766231
    in windows form application you can use 'AutoCompleteMode of combobox to accomplish your task, just follow the steps
    1. Create one Windows Application and add DataGridView from toolbox to design.
    2. Now create two DataGridViewComboBoxColumns and add them to the DataGridView:

    public void ComboList1()
    {
    DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
    combo1.HeaderText = "fruit";
    combo1.Items.Add("Apple");
    combo1.Items.Add("Grapes");
    combo1.Items.Add("Orange");
    combo1.Items.Add("Mango");
    dataGridView1.Columns.Add(combo1);
    }

    call above method in form constructor
    Now Click on DataGridView and generate EditingControlShowing event and write the folllowing code in it:

    if (e.Control is DataGridViewComboBoxEditingControl)
    {
    ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
    ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
    ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
    }

    The AutoCompleteMode and AutoCompleteSource properties create a ComboBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments