How to suggest adding a ComboBox to a DataGridView?

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

But if I install AutoCompleteMode in SuggestAppend in the DataGridView ComboBox , it does not add text (Fig. 2).

How to enable SuggestAppend in datagridview combo box?

Fig. one:

AutoComplete ComboBox

Fig. 2:

AutoComplete DataGridViewComboBoxCell

+7
c # winforms combobox datagridview
source share
2 answers

You think you will do it like a regular 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 expected results:

AutoComplete ComboBox

As it turned out, you can! But the selected option will not be saved as soon as you leave the cell. I found that you need to change the way you add drop-down options and the way they source:

 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 give you the desired results:

AutoComplete DataGridViewComboBoxCell

+4
source share

Here is a quick example of using ComboBox AutoComplete in a DataGridView in a Windows application.

Create a single Windows application and add a DataGridView from the toolbar to the project. 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 of these methods from the form constructor.

Now click on the DataGridView and create the EditingControlShowing event and write the following 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 that are present in the DataGridView.

Got from this post .

0
source share

All Articles