我在ComboBox
Windows窗体应用程序中有一个c#
,我将AutoCompleteMode
设置为SuggestAppend
,文本会自动附加到输入中(图1)
但如果我在AutoCompleteMode
中将SuggestAppend
设置为DataGridView ComboBox
,则不会附加文字(图2)。
如何在datagridview组合框中启用SuggestAppend
?
图1:
图2:
答案 0 :(得分:5)
你认为你会像普通的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;
期待结果:
事实证明,你可以!但是,一旦离开单元格,选定的选项将不会保留。我发现你必须更改如何添加下拉选项以及如何获取它们:
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;
}
}
这将为您提供所需的结果:
答案 1 :(得分:0)
以下是如何在Windows应用程序中的DataGridView中使用AutoComplete ComboBox的快速示例。
创建一个Windows应用程序并将DataGridView从工具箱添加到设计中。 现在创建两个DataGridViewComboBoxColumns并将它们添加到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);
}
从Form Constructor中调用这两个方法。
现在单击DataGridView并生成EditingControlShowing事件并在其中编写以下代码:
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;
}
这适用于DataGridView中的所有组合框。
来自this post。