Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Can DataGridViewComboBoxCell be able to edit by user (like a normal Combobox)?
 

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!
Sambur  Tuesday, November 29, 2005 3:05 AM

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"

 


 

Mark Rideout  Thursday, December 01, 2005 2:08 AM

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  Monday, June 12, 2006 12:18 PM

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"

 


 

Mark Rideout  Thursday, December 01, 2005 2:08 AM

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  Thursday, December 01, 2005 9:18 AM

To Sambur:

    I have a same trouble with you.

csharp2000  Thursday, December 15, 2005 1:30 PM

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�o:p>

 

 

Mark Rideout  Thursday, December 15, 2005 6:50 PM

My DataGridView is not databound

 

 

csharp2000  Friday, December 16, 2005 12:43 AM
My combo box column is not databound.
Sambur  Friday, December 16, 2005 3:32 AM

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  Monday, June 12, 2006 12:18 PM

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

Alan Ren  Friday, June 23, 2006 4:05 AM
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  Saturday, January 27, 2007 9:55 AM

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  Tuesday, January 30, 2007 9:14 AM
Do you have this code in VB? Thanks
pmak  Friday, July 20, 2007 10:11 PM

You can use google to search for other answers

Custom Search

More Threads

• Change table orientation in DataGridView
• How to get last value for identity column
• TextBox typing vs. programatic change
• Binding Grdview to BindingSource
• Freezing columns in DataGrid control?
• Rows added in DataGridView.OnRowStateChanged are not selectable
• What's the equivalent of OnItemDataBound?
• How to make the back color of the cells permenent?
• two datagridviewcombobox columns in datagridview
• How to get the correct value for DataGrid.IsSelected(index) after DataView.Sort