C# How to autocomplete specific Gridview column on matching part of any string

Viewed 25
  • I have 3 column Gridview in C# Winform and there are list of drugs in the MS SQL database table.
  • I want to add autocomplete feature to the second column of the gridview with the data from the drugList table.
  • It should show the all drugs in the auto complete list, if typed string is included in anywhere in the drugList entry.

This is what I'm trying and it doesn't show any output.

private void gvMedicationsTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            string val = textBox.Text;
            cDrugs drug = new cDrugs();
            string[] autocompleteList = drug.GetAutoCompleteDrugsList(val);
            if (autocompleteList != null)
            {
                AutoCompleteStringCollection dataCollection = new AutoCompleteStringCollection();
                dataCollection.AddRange(autocompleteList);
                textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox.AutoCompleteCustomSource = dataCollection;
            }
            textBox.Text = val;

        } 

This is event registration

    private void gvMedications_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {            
        
                    TextBox textBox = e.Control as TextBox;
                    if (textBox != null)
                    {
                        textBox.TextChanged += new EventHandler(gvMedicationsTextBox_TextChanged);               
                    }
}
1 Answers

You can add a combobox column to the grid and enable the AutoComplete property so that the selected value will change automatically when anything is written in the cell.

Related