我知道在正常ComboBox
中,如果FlatStyle
为Standard
,则用户将能够键入不在Items
列表中的值。但是,如果DataGridView
中的组合框为Standard
,则不会让我输入新值。是否可以在DataGridView
?
答案 0 :(得分:1)
好吧,在DataGridView中,您可以添加DataGridViewComboBoxColumn
类型的列。它是DisplayStyle
和/或FlatStyle
取决于当前行状态。我想当你添加一个新行(编辑模式)时,你可以为它添加值。
参考文献: Add items to DataGridViewComboBoxColumn in DataGridView during runtime http://www.lazycoder.com/weblog/2006/09/12/adding-values-to-the-datagridviewcomboboxcell-at-runtime/
答案 1 :(得分:0)
private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
switch (dataGrid_ItemsList.Columns[dataGrid_ItemsList.SelectedCells[0].ColumnIndex].HeaderText)
{
case "Batch":
if (e.Control is ComboBox)
{
ComboBox _with1 = (ComboBox)e.Control;
_with1.DropDownStyle = ComboBoxStyle.DropDown;
_with1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
_with1.AutoCompleteSource = AutoCompleteSource.CustomSource;
_with1.AutoCompleteCustomSource = BatchList;
//_with1.Validating -= HandleComboBoxValidating;
//_with1.Validating += HandleComboBoxValidating;
_with1.Validating += (ss, ee) =>
{
if (!_with1.Items.Contains(_with1.Text))
{
var comboColumn = dataGrid_ItemsList.CurrentCell as DataGridViewComboBoxCell;
_with1.Items.Add(_with1.Text);
_with1.Text = _with1.Text;
comboColumn.Items.Add(_with1.Text);
this.dataGrid_ItemsList.CurrentCell.Value = _with1.Text;
}
};
}
break;
}
}
catch (Exception ex)
{
_CommonClasses._Cls_ExceptionsHandler.HandleException(ex,false);
}
}