目标是在dgv列下键入任何单元格以进行自动完成。但是在输入' t'后,列表不会出现。添加断点后,我发现当我选择一个单元格时,它在我开始输入之前就触发了EditControlShowing事件处理程序。当我输入时,EditingControlShowing代码永远不会再次进入。
我确实有其他单元格点击和单元格输入事件处理程序链接到相同的dgv。但我怀疑他们是否干涉了这一点。
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int rowIndex = dataGridView.CurrentCell.RowIndex;
int columnIndex = dataGridView.CurrentCell.ColumnIndex;
string columnName = dataGridView.Columns[columnIndex].Name;
if (columnName == "ColumnName")
{
var box = (TextBox)e.Control;
box.AutoCompleteCustomSource = new AutoCompleteStringCollection();
box.AutoCompleteCustomSource.AddRange(new string[] { "Text1", "Text2", "Text3" });
box.AutoCompleteMode = AutoCompleteMode.Suggest;
box.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}