Edit text in a DataGridViewComboBoxColumn

Hi all,

How can I edit a text in a DataGridViewComboBoxColumn?

BR / Chris

[93 byte] By [ChristianBurgener] at [2008-1-4]
# 1
By default a DataGridViewComboBoxCell does not support typing into the cell. There are reasons though that typing into the combo box works well for your application. To enable this, two things have to be done. First the DropDownStyle property of the ComboBox editing control needs to be set to DropDown to enable typing in the combo box. The second thing that needs to be done is to ensure that the value that the user typed into the cell is added to the combo box items collection. The appropriate place to add the value to the items collection is in the CellValidating event handler.

Code Snippet


private
void dataGridView1_CellValidating(object sender,

DataGridViewCellValidatingEventArgs e)

{

if (e.ColumnIndex == comboBoxColumn.DisplayIndex)

{

if (!this.comboBoxColumn.Items.Contains(e.FormattedValue))

{

this.comboBoxColumn.Items.Add(e.FormattedValue);

}

}

}

private void dataGridView1_EditingControlShowing(object sender,

DataGridViewEditingControlShowingEventArgs e)

{

if (this.dataGridView1.CurrentCellAddress.X == comboBoxColumn.DisplayIndex)

{

ComboBox cb = e.Control as ComboBox;

if (cb != null)

{

cb.DropDownStyle = ComboBoxStyle.DropDown;

}

}

}

Zhi-XinYe-MSFT at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Hi Zhi-Xin,

Thanks for your answer.

I’m working with a gridview in a databound scenario. The entered value will not be accepted this way.

BR / Chris

ChristianBurgener at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Christian Burgener wrote:

Hi Zhi-Xin,

Thanks for your answer.

I’m working with a gridview in a databound scenario. The entered value will not be accepted this way.

BR / Chris

"GridView"? Sounds like you're using ASP.NET, if so, I recommend you post this question at http://forums.asp.net for better responses.

Zhi-XinYe-MSFT at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
hi all,

I am facing the problem as while applying this code.

my datagridviewcombocolumn is bound to list...so it does not allow to add item in Dropdown items.I get exception as "System.ArgumentException was unhandled by user code Message="Items collection cannot be modified when the DataSource property is set."
nil at 2008-1-17 > top of Msdn Tech,Windows Forms,Windows Forms General...