如何在 DataGridView 中建议追加 ComboBox? [英] How to Suggest Append ComboBox in DataGridView?
问题描述
我在 c#
Windows 窗体应用程序中有一个 ComboBox
,我已将 AutoCompleteMode
设置为 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).
但是如果我在 DataGridView ComboBox
中将 AutoCompleteMode
设置为 SuggestAppend
,它不会附加文本(图 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?
图一:
图2:
推荐答案
你会认为你会像普通的 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;
预期结果:
事实证明,你可以!但是,一旦您离开单元格,所选的选项就不会保留.我发现您必须更改如何添加下拉选项以及如何获取它们:
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屋!