Is it possible to have a column in a DataGridView that works like a ComboBox that allows the user to enter a new value?

I know that in a regular ComboBox , if FlatStyle is Standard , the user will be able to enter a value that is not in the Items list. But if the combo box in the DataGridView is Standard , it will not let me enter a new value. Is it possible to achieve this functionality in a column in a DataGridView ?

+4
source share
2 answers

Well, in a DataGridView you can add a column of type DataGridViewComboBoxColumn . It DisplayStyle and / or FlatStyle depends on the current state of the row. I think that when you add a new line (edit mode), you can add values ​​to it.

References: Add items to a DataGridViewComboBoxColumn to a DataGridView at runtime http://www.lazycoder.com/weblog/2006/09/12/adding-values-to-the-datagridviewcomboboxcell-at-runtime/

+1
source
  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); } } 
0
source

All Articles