I am trying to enter values in a datagridview Combobox. but it does not Allows. What to do?
Asked
Active
Viewed 1.1k times
3 Answers
8
private void GridStockItemEntry_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewRow row = GridStockItemEntry.CurrentRow;
DataGridViewCell cell = GridStockItemEntry.CurrentCell;
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
if (cell == row.Cells["ItemName"] && Convert.ToString(row.Cells["Type"].Value) == "Raw Material")
{
DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;
cbo.DropDownStyle = ComboBoxStyle.DropDown;
cbo.Validating += new CancelEventHandler(cbo_Validating);
}
}
}
void cbo_Validating(object sender, CancelEventArgs e)
{
DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
DataGridView grid = cbo.EditingControlDataGridView;
object value = cbo.Text;
// Add value to list if not there
if (cbo.Items.IndexOf(value) == -1)
{
DataGridViewComboBoxCell cboCol = (DataGridViewComboBoxCell)grid.CurrentCell;
// Must add to both the current combobox as well as the template, to avoid duplicate entries...
cbo.Items.Add(value);
cboCol.Items.Add(value);
grid.CurrentCell.Value = value;
}
}
-
cbo_validating() is called continuously even when i am setting e.cancel as true. is there any specific reason? – gulshanm01 Apr 03 '14 at 12:59
5
Maybe, this example is better readable:
private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
DataGridView dgv = (DataGridView)sender;
if(dgv.CurrentCell.ColumnIndex==dgv.Columns["ColumnName"].Index) {
ComboBox cbx = (ComboBox)e.Control;
cbx.DropDownStyle = ComboBoxStyle.DropDown;
cbx.AutoCompleteSource = AutoCompleteSource.ListItems;
cbx.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
}
}

termigrator
- 159
- 3
- 11
-2
Make sure that EditMode
property of the DataGridView
is set to EditOnKeystrokeOrF2
Also, verify that ReadOnly
property is set to False
.

Chris Ballard
- 3,771
- 4
- 28
- 40

tzup
- 3,566
- 3
- 26
- 34
-
-
What doesn't allow for combobox? I just tried a gridview with these properties AND a combobox in a column. – tzup Feb 11 '11 at 13:15
-
1this options will not work datagridview. as per your answer it will be only allow to select value from combobox, it will not allow to enter text – Imran Ali Khan Apr 01 '15 at 06:06