Can DataGridViewComboBoxCell be able to edit by user (like a normal Combobox)?

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!
[162 byte] By [Sambur] at [2007-12-17]
# 1

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"


MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

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;
}
}

Sambur at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

To Sambur:

I have a same trouble with you.

csharp2000 at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

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”

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

My DataGridView is not databound

csharp2000 at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
My combo box column is not databound.
Sambur at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

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;

}

}

caiman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

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

AlanRen at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9
Alan,
What happens is that the new string needs to be added to the Value member of the DataGridViewComboBoxCell in order to be displayed. But it is typically not enough if the content has been edited by the user: you also need to implement several steps to make it display immediately. There is a good example of how to implement the embedded ComboBox on this link:
http://www.sommergyll.com/datagridview-usercontrols/datagridview-with-combobox.htm
hope it helps

Cheers

PureOrange at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10

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

Prasenna at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 11
Do you have this code in VB? Thanks
pmak at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...