如何建议在DataGridView中附加ComboBox? [英] How to Suggest Append ComboBox in DataGridView?
问题描述
我有一个 ComboBox
在 c#
Windows窗体应用程序中,我已经设置了 AutoCompleteMode
to SuggestAppend
,文字会自动附加到输入(图1)。
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).
但是如果将 AutoCompleteMode
设置为在
DataGridView ComboBox $ c $中的SuggestAppend
c>它不附加文本(图2)。
But if I set AutoCompleteMode
to SuggestAppend
in a DataGridView ComboBox
it does not append the text (Fig 2).
如何在datagridview组合框中启用 SuggestAppend
?
How can I enable SuggestAppend
in a datagridview combobox?
图1:
图2:
推荐答案
d认为你会像普通的 ComboBox
一样做:
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;
预期结果:
< img src =https://i.stack.imgur.com/itDx5.pngalt =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:
这篇关于如何建议在DataGridView中附加ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!