Can DataGridViewComboBoxCell be able to edit by user (like a normal Combobox)?
If can't, how to customize a cell/column to get this feature?
Thanks a lot!
The DataGridView does not support this by default. You'll need to customize parts to get this to work. Check out the DataGridView FAQ for more details: http://www.windowsforms.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Hi,
I modified my code as attached, now I can type in the combobox cell and the data was added to the items list, but when I leave the editing cell, the data disappeared from the text display area of the cell (nothing displayed). what's wrong? thanks a lot.
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.CurrentCell.IsInEditMode)
{
if (dataGridView1.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
{
if (!((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Contains(e.FormattedValue))
{
((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(e.FormattedValue);
}
}
}
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
}
}
Is your DataGridView databound? What about your combo box column -is it databound? If the combo box column is databound then you need to add the new values to the combo box columns datasource, not the items collection.
-mark
DataGridView Program Manager
Microsoft
This post is provided “as-is”
Hi,
you have an error in your code. You must add the new item to the items collection of the Cell and not to the items of the entire column.
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;
//you must now implement CellValidated and set there the value of the cell again
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;
}
}
I have a question on DataGridViewComboBoxCell, I can edit it and add value on it. but after it the combobox cell is display a blank not the value i just added. it happens when the form are first loaded.
what should i do to make the combobox display the first value of the datasource.
thanks a lot
Cheers
hi all,
http://www.windowsforms.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc
this link was very usefull, i have a combo box, its a databound now i want the combo box to be editable, and i have to check whether the new entry is not available in the databound, if not available should store into the database, how to validate this requirements, helps please.
Regards,
Prasenna. K