DataGridViewComboBox - How to resolve any value?

I am having problems with VisualStudio 2010 C # Winforms.

I created a DataGridView with an unrelated column of type DataGridViewComboBoxColumn. The column works fine, except, unlike the usual ComboBox, I seem to be unable to enter just any value. I am forced to select a value from a list.

Is there a property that I need to set, or another type that I can use that will allow me to enter any value in the cell in addition to providing a list to select the value from?

Thanks!

+7
c # winforms
source share
2 answers

I don't think there is a property that will allow this, but I found an answer here that worked with a little modification.

Try adding the following 2 event handlers, a column called comboBoxColumn is assumed here:

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox c = e.Control as ComboBox; if (c != null) c.DropDownStyle = ComboBoxStyle.DropDown; } private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == comboBoxColumn.Index) { object eFV = e.FormattedValue; if (!comboBoxColumn.Items.Contains(eFV)) { comboBoxColumn.Items.Add(eFV); dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV; } } } 
+10
source share

The DataGridViewComboBoxColumn is intended to be used to select from possible values, rather than to enter data. If you want to add any data, you must do this programmatically for the desired DataGridViewComboBoxCell:

 ((DataGridViewComboBoxCell)dataGridView1[0,0]).Items.AddRange(new string [] {"A","B","C"}); 
0
source share

All Articles