9

I have a ComboBox in a c# Windows forms application where I have set AutoCompleteMode to SuggestAppend, and the text is automatically appended to the input (Fig 1).

But if I set AutoCompleteMode to SuggestAppend in a DataGridView ComboBox it does not append the text (Fig 2).

How can I enable SuggestAppend in a datagridview combobox?

Fig 1 :

AutoComplete ComboBox

Fig 2 :

AutoComplete DataGridViewComboBoxCell

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Tharif
  • 13,794
  • 9
  • 55
  • 77

2 Answers2

7

You'd think you'd do it just like the normal ComboBox:

this.comboBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection();
this.comboBox1.AutoCompleteCustomSource.AddRange(new string[] { "Good night", "Good evening", "Good", "All Good", "I'm Good" });
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

With the expectant results:

AutoComplete ComboBox

As it turns out, you can! But the selected option won't persist once you leave the cell. I found you have to change how you add the drop-down options and how you source them:

public Form1()
{
  InitializeComponent();
  DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn();
  cc.Name = "Combo";
  cc.Items.AddRange(new string[] { "Good night", "Good evening", "Good", "All Good", "I'm Good" });
  this.dataGridView1.Columns.Add(cc);
}

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  ComboBox box = e.Control as ComboBox;
  if (box != null)
  {
    box.DropDownStyle = ComboBoxStyle.DropDown;
    box.AutoCompleteSource = AutoCompleteSource.ListItems;
    box.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  }
}

This will provide you the desired results:

AutoComplete DataGridViewComboBoxCell

Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • thank you @OhBeWise for your time,actually i wrote the same code but instead suggestAppend i wrote only suggest :) – Tharif May 07 '15 at 04:10
  • 1
    This solved my problem in a very complex program structure. I want to add my solution so maybe it can help others too. I had a long list of Combobox and just want to add a suggestion from my list with typing instead of fixed Combobox. Added 3 lines of your code inside my EditingControlShowing under the code which have had in it. `box.DropDownStyle = ComboBoxStyle.DropDown; box.AutoCompleteSource = AutoCompleteSource.ListItems; box.AutoCompleteMode = AutoCompleteMode.SuggestAppend;` – Alper Dec 25 '20 at 11:28
0

Here is a quick example of how to use an AutoComplete ComboBox in DataGridView in a Windows Application.

Create one Windows Application and add DataGridView from toolbox to design. Now create two DataGridViewComboBoxColumns and add them to the DataGridView:

public void ComboList1()
        {
            DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
            combo1.HeaderText = "Country";
            combo1.Items.Add("Antarctica");
            combo1.Items.Add("Belgium");
            combo1.Items.Add("Canada");
            combo1.Items.Add("Finland");
            combo1.Items.Add("Albania");
            combo1.Items.Add("India");
            combo1.Items.Add("Barbados");
            dataGridView1.Columns.Add(combo1);
        } 
public void ComboList2()
        {
            DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
            combo2.HeaderText = "Types of Jobs";
            combo2.Items.Add("Accounting");
            combo2.Items.Add("HR");
            combo2.Items.Add("Finance");
            combo2.Items.Add("Transportation");
            combo2.Items.Add("Testing");
            dataGridView1.Columns.Add(combo2);
        }

Call both these methods from the 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;
            }

enter image description here

This will work for all comboBoxes which are present in the DataGridView.

Got from this post.

Community
  • 1
  • 1
vaheeds
  • 2,594
  • 4
  • 26
  • 36