Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...