您必须更改组合框 DropDownStyle 以允许用户在组合框处于编辑模式时输入文本,但标准 DataGridView 在设计时不允许此行为,因此您必须采取一些技巧,处理 CellValidating、EditingControlShowing 和 CellValidated事件。
这是代码(来自 MSDN 论坛,对我有用)。
private Object newCellValue;
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.CurrentCell.IsInEditMode)
{
if (dataGridView1.CurrentCell.GetType() ==
typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cell =
(DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (!cell.Items.Contains(e.FormattedValue))
{
cell.Items.Add(e.FormattedValue);
cell.Value = e.FormattedValue;
//keep the new value in the member variable newCellValue
newCellValue = e.FormattedValue;
}
}
}
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() ==
typeof(DataGridViewComboBoxEditingControl))
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
}
}
private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.GetType() ==
typeof(DataGridViewComboBoxCell))
{
DataGridViewCell cell =
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = newCellValue;
}
}