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 :
Fig 2 :
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:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With