您可以使用的一个选项是自动完成。这可以模拟 DataGridViewTextCell 上的大多数所需行为,除了在输入文本单元格时显示所有选项,并且无需将单元格类型转换为 ComboBox。
这可以在 DataGridView EditingControlShowing 事件中处理:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
TextBox box = e.Control as TextBox;
box.AutoCompleteCustomSource = new AutoCompleteStringCollection();
box.AutoCompleteCustomSource.AddRange(new string[] { "Foo", "Bar", "Baz" });
box.AutoCompleteMode = AutoCompleteMode.Suggest;
box.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}

给定,用户必须输入文本才能显示任何选项。如果所需的行为需要在输入文本框时显示所有选项,那么这不是您的最佳选择。但是,如果这对所有其他必需的行为(建议的选项、接受非选项条目、不必总是显示等)次要,那么这是一个可行的解决方案。
编辑
这适用于以下所有情况:
- DataGridView 是数据绑定的。
绑定数据源:
public BindingList<Example> Examples { get; set; }
this.Examples = new BindingList<Example>();
dataGridView1.DataSource = this.Examples;
其中 Example 是一个简单的类:
public class Example
{
public string First { get; set; }
public string Last { get; set; }
public string Test { get; set; }
}
- 手动添加列。
只是一个空列:
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = "Extra";
col.HeaderText = "Extra";
this.dataGridView1.Columns.Add(col);
- 1 和 2 合二为一。