Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Suggest Append ComboBox in DataGridView?

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

like image 698
Tharif Avatar asked May 06 '15 11:05

Tharif


1 Answers

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

like image 110
OhBeWise Avatar answered Oct 13 '22 01:10

OhBeWise